编程语言
首页 > 编程语言> > C# ——键盘指定输入

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;

 

 

标签:Trim,return,sender,C#,Text,KeyChar,键盘,输入,string
来源: https://www.cnblogs.com/eve612/p/14530365.html