textbox输入数字

来源:互联网 发布:java程序员进阶书籍 编辑:程序博客网 时间:2024/04/29 15:57

  private void txtVaInt_KeyPress(object sender, KeyPressEventArgs e)
        {
   if ((e.KeyChar == 3) || (e.KeyChar == 24) || (e.KeyChar == 22))
                {
                    return;
                }
                if ((e.KeyChar < 48 || e.KeyChar > 57) && e.KeyChar != 8 && e.KeyChar != 13 && e.KeyChar != 45)
                {
                    e.Handled = true;
                }
                if (e.KeyChar == 45 && (((TextBox)sender).SelectionStart != 0 || ((TextBox)sender).Text.IndexOf("-") >= 0))
                {
                    e.Handled = true;
                }
                if (e.KeyChar == 48 && (((TextBox)sender).SelectionStart == 0))
                {
                    e.Handled = true;
                }

}