限制只能输入数字:

private void textBox1_KeyPress(object sender, KeyPressEventArgs e)

{

            if ((e.KeyChar >= '0' && e.KeyChar <= '9')  || (byte)(e.KeyChar) == 8)//8就是回格,backspace(删除).
            {
            }
            else
            {
                e.Handled = true;
            }

}

限制只能输入数字和一个小数点:

要限制输入的textbox的Keypress事件:

private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if ((e.KeyChar >= '0' && e.KeyChar <= '9') || e.KeyChar == '.' || (byte)(e.KeyChar) == 8)//8就是回格,backspace(删除).
            {
                if (e.KeyChar == '.' && (sender as TextBox).Text.IndexOf('.') >= 0)
                {
                    e.Handled = true;
                }
            }
            else
            {
                e.Handled = true;
            }
        }

posted on 2012-11-08 21:21  Asa.Zhu  阅读(4243)  评论(0编辑  收藏  举报