C# ——键盘指定输入

防止输入有误

 private void txtC_KeyPress(object sender, KeyPressEventArgs e)
        {
            txtKeyPress(sender, e);
        }

        private void txtKeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 0x20) e.KeyChar = (char)0;  //禁止空格键  
            if ((e.KeyChar == 0x2D) && (((TextBox)sender).Text.Length == 0)) return;   //处理负数  
            if (e.KeyChar > 0x20)
            {
                try
                {
                    double.Parse(((TextBox)sender).Text + e.KeyChar.ToString());
                }
                catch
                {
                    e.KeyChar = (char)0;   //处理非法字符  
                }
            }
        }

        public bool IsValid()
        {
            if (string.IsNullOrEmpty(this.txtA.Text.Trim()) || string.IsNullOrEmpty(this.txtB.Text.Trim()) || string.IsNullOrEmpty(this.txtC.Text.Trim()))
            {
                MessageBox.Show("直线方程参数不能为空");
                return false;
            }
            return true;

 

 

posted @ 2021-03-13 21:00  vv彭  阅读(225)  评论(0编辑  收藏  举报