C#textBox文本框限制

1.正整数

//可以输入正整数 
        private void textBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (((int)e.KeyChar < 48 || (int)e.KeyChar > 57) && (int)e.KeyChar != 8) { e.Handled = true; }
        }

2.整数

private void textBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (((int)e.KeyChar < 48 || (int)e.KeyChar > 57) && (int)e.KeyChar != 8 && (int)e.KeyChar != 45) { e.Handled = true; }
            if ((int)e.KeyChar == 45)
            {
                //如果选中文本框中的所有文本,则可以输入负号
                if (textBox.SelectionLength == textBox.TextLength) { }
                else
                {
                    //如果文本框的第一个字符被选中,则可以输入负号
                    if (textBox.SelectionStart == 0) { }
                    else
                    {
                        if (textBox.TextLength != 0)
                        {
                            e.Handled = true;
                        }
                    }
                }
            }
        }

3.超出范围限制自动退格

可以用两种方法触发:
1.textBox_TextChange:每输入一个字符,都会验证
2.textBox_KeyDown:当摁下某个键时,验证(下例是回车键)

//可以输入小于100的整数
        private void textBox_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (textBox.Text != "" && textBox.Text != "-")
                {
                    if (Convert.ToInt32(textBox.Text) < 100)
                    {
                        MessageBox.Show("请输入小于100的整数", "提示");
                        string str = textBox_ControlRobot_TravelAngleControl_TravelDistance.Text;
                        str = str.Substring(0, str.Length - 1);
                        textBox.Text = str;
                        textBox.SelectionStart = textBox.TextLength;
                    }
                }
            }
        }

4.只能输入汉字

输入其它字符没有反应

private void txtRealName_KeyPress(object sender, KeyPressEventArgs e)
        {
            Regex rg = new Regex("^[\u4e00-\u9fa5]$");
            if (!rg.IsMatch(e.KeyChar.ToString()) && e.KeyChar != '\b')
            {
                e.Handled = true;
            }
        }

5.只能输入字母和数字

private void txtPass_KeyPress(object sender, KeyPressEventArgs e)
        {
            if ((e.KeyChar >= 'a' && e.KeyChar <= 'z') || (e.KeyChar >= 'A' && e.KeyChar <= 'Z')
                || (e.KeyChar >= '0' && e.KeyChar <= '9') || (e.KeyChar == 8))
            {
                e.Handled = false;
            }
            else
            {
                e.Handled = true;
            }
        }

6.不能少于8位

private void txtPass_Validating(object sender, CancelEventArgs e)
        {
            if (txtPass.TextLength < 8)
            {
                MessageBox.Show("密码不能小于8位", "提示");
                return;
            }
        }
  • 0
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值