C#控件中只允许输入数字

        private void OnlyNum_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
        {
            if (!(Char.IsNumber(e.KeyChar) || e.KeyChar == '\b'))
            {
                e.Handled = true;
            }
        }

 

\b 表示退格符, ASCII  \u0008

e.Handled = true; //设置已处理过该键值,相当于忽略输入

e.KeyChar = '*';  //还可添加这句,让输入的字符都变成'*'

想要设置的控件的KeyPress 都指向这个过程

posted on 2008-03-06 18:04  Lain  阅读(688)  评论(0)    收藏  举报

导航