WinForm textbox only allows the input digital

input textbox only a specified number (1-7), and can not be repeated

Implemented in the KeyPress event

 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

 

Guess you like

Origin www.cnblogs.com/ZJ199012/p/11726517.html