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

 

posted on 2019-10-23 15:25  lovezj9012  阅读(209)  评论(0编辑  收藏  举报

导航