WinForm textbox 只允许输入数字

textbox只允许输入指定数字(1-7),并且不能重复

在KeyPress事件中实现

 private void txtWeek_KeyPress(object sender, KeyPressEventArgs e)
        {
            if ((e.KeyChar < '1' || e.KeyChar > '7') && e.KeyChar != (char)8) //允许输入退格键
            {
                e.Handled = true;
            }
            if (((TextBox)sender).Text.Trim().IndexOf(e.KeyChar)>=0)
            {
                e.Handled = true;
            }
        }
View Code

猜你喜欢

转载自www.cnblogs.com/ZJ199012/p/11726517.html