您现在的位置: 365建站网 > 365文章 > C#/vb.net中WinForm TextBox 只能输入数字的实例

C#/vb.net中WinForm TextBox 只能输入数字的实例

文章来源:365jz.com     点击数:2075    更新时间:2018-06-13 08:56   参与评论

       在Winform(C#)中要实现限制Textbox只能输入数字,一般的做法就是在按键事件中处理,判断keychar的值。限制只能输入数字,小数点,Backspace,del这几个键。数字0~9所对应的keychar为48~57,小数点是46,Backspace是8。    
      拖一个Textbox到窗体上,添加OnKeyPress事件,在事件写判断的代码,只要判断不是这些键,设置e.Handled的值为true,就可以屏蔽输入。
1.判断是否为数字或Backspace,按下面这样写的话只能输入数字和Backspace,所以还得给代码添加些条件,还要能够输入小数点。
2.输入小数点。输入的小数要符合数字的格式,类似9.9.9这样的是不能够输入的。做法就是用float.TryParse来转换Textbox中之前和之后的值,然后比较两者的转换结果。

1.C#代码

在Winform

</>code

  1. private void textBox1_KeyPress(object sender, KeyPressEventArgs e)  
  2.         {  
  3.             //数字0~9所对应的keychar为48~57,小数点是46,Backspace是8  
  4.             e.Handled = true;  
  5.             //输入0-9和Backspace del 有效  
  6.             if ((e.KeyChar >= 47 && e.KeyChar <= 58) || e.KeyChar == 8)  
  7.             {  
  8.                 e.Handled = false;  
  9.             }  
  10.             if (e.KeyChar == 46)                       //小数点        
  11.             {  
  12.                 if (textBox1.Text.Length <= 0)  
  13.                     e.Handled = true;           //小数点不能在第一位        
  14.                 else  
  15.                 {  
  16.                     float f;  
  17.                     if (float.TryParse(textBox1.Text + e.KeyChar.ToString(), out f))  
  18.                     {  
  19.                         e.Handled = false;  
  20.                     }  
  21.                 }  
  22.             }  
  23.         }


</>code

  1. 方法一:
  2. private void tBox_KeyPress(object sender, KeyPressEventArgs e)
  3.  {
  4.             if (e.KeyChar == 0x20) e.KeyChar = (char)0;  //禁止空格键
  5.             if ((e.KeyChar == 0x2D) && (((TextBox)sender).Text.Length == 0)) return;   //处理负数
  6.             if (e.KeyChar > 0x20)
  7.             {
  8.                 try
  9.                 {
  10.                     double.Parse(((TextBox)sender).Text + e.KeyChar.ToString());
  11.                 }
  12.                 catch
  13.                 {
  14.                     e.KeyChar = (char)0;   //处理非法字符
  15.                 }
  16.             }
  17. }
  18. 方法二:
  19. private void TextBox_KeyPress(object sender, KeyPressEventArgs e)
  20.  {
  21.     if(e.KeyChar!=8&&!Char.IsDigit(e.KeyChar))
  22.     {
  23.       e.Handled = true;
  24.     }
  25. }
  26. 或者
  27. private void TextBox_KeyPress(object sender, KeyPressEventArgs e)
  28. {
  29.     if(e.KeyChar!='\b'&&!Char.IsDigit(e.KeyChar))
  30.     {
  31.       e.Handled = true;
  32.     }
  33. }
  34. 方法三:
  35. private void textBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
  36. {
  37. if(e.KeyChar!='\b')//这是允许输入退格键
  38. {
  39. if((e.KeyChar<'0')||(e.KeyChar>'9'))//这是允许输入0-9数字
  40. {
  41. e.Handled = true;
  42. }
  43. }
  44. }
  45. 方法四:
  46. private void textBox1_Validating(object sender, CancelEventArgs e) 
  47. const string pattern = @"^\d+\.?\d+{1}quot;; 
  48. string content = ((TextBox)sender).Text; 
  49. if (!(Regex.IsMatch(content, pattern))) 
  50. errorProvider1.SetError((Control)sender, "只能输入数字!"); 
  51. e.Cancel = true; 
  52. else 
  53. errorProvider1.SetError((Control)sender, null); 
  54. }
  55. 方法五:
  56. private void textBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
  57. {
  58. if(e.KeyChar=='.' && this.textBox1.Text.IndexOf(".")!=-1)
  59. {
  60. e.Handled=true;
  61. }
  62. if(!((e.KeyChar>=48 && e.KeyChar<=57) || e.KeyChar=='.' || e.KeyChar==8))
  63. {
  64. e.Handled=true;
  65. }
  66. }
  67. 方法六:
  68. private void tbx_LsRegCapital_KeyPress(object sender, KeyPressEventArgs e)
  69. {
  70.             if (!Char.IsNumber(e.KeyChar) && !Char.IsPunctuation(e.KeyChar) && !Char.IsControl(e.KeyChar))
  71.             {
  72.                 e.Handled = true;//消除不合适字符
  73.             }
  74.             else if (Char.IsPunctuation(e.KeyChar))
  75.             {
  76.                 if (e.KeyChar != '.' || this.textBox1.Text.Length == 0)//小数点
  77.                 {
  78.                     e.Handled = true;
  79.                 }
  80.                 if (textBox1.Text.LastIndexOf('.') != -1)
  81.                 {
  82.                     e.Handled = true;
  83.                 }
  84.             }      
  85.   }  
  86. 方法七:
  87. 利用ASCII码处理办法、
  88. {
  89.             if ((e.KeyChar <= 48 || e.KeyChar >=57) && (e.KeyChar != 8) && (e.KeyChar != 46))
  90.               e.Handled = true;
  91. ================48代表0,57代表9,8代表空格,46代表小数点
  92. }


C#中 textbox只允许整数输入

Key Press事件

</>code

  1. private void textBox1_KeyPress(object sender, KeyPressEventArgs e)  
  2. {  
  3.      if (e.KeyChar >= 31 && (e.KeyChar < '0' || e.KeyChar > '9')) { e.Handled = true; }  
  4. }

 

Textbox 内限定输入只有两位小数的数字

</>code

  1. private void 只允许输入两位小数数字(object sender, KeyPressEventArgs e)  
  2. {  
  3.     //设置只允许输入  .  -  0~9  否则无效  
  4.     if ((e.KeyChar < 48 || e.KeyChar > 57) && e.KeyChar != 8 && e.KeyChar != 13 && e.KeyChar != 45 && e.KeyChar != 46)  
  5.         e.Handled = true;  
  6.     //输入为负号时,只能输入一次且只能输入一次  
  7.     if (e.KeyChar == 45 && (((TextBox)sender).SelectionStart != 0 || ((TextBox)sender).Text.IndexOf("-") >= 0))  
  8.         e.Handled = true;  
  9.     //输入小数点时判断  
  10.     if (e.KeyChar == 46)  
  11.     {  
  12.         //负数判断  
  13.         if (((TextBox)sender).Text.IndexOf("-") >= 0)  
  14.         {  
  15.             //如果有-号 输入为小数点时,小数点只能在负号右边第二位以上才能输入.只能输入一次  
  16.             if (((TextBox)sender).SelectionStart <= 1 || ((TextBox)sender).Text.IndexOf(".") >= 0) e.Handled = true;  
  17.         }  
  18.         else  
  19.         {  
  20.             //第二位开始允许输入小数,且只能输入一次  
  21.             if (((TextBox)sender).SelectionStart <= 0 || ((TextBox)sender).Text.IndexOf(".") >= 0) e.Handled = true;  
  22.         }  
  23.         //限定小数点出现位置,防止出现三位以上小数的情况  
  24.         if (((TextBox)sender).SelectionStart < ((TextBox)sender).Text.Length - 2) e.Handled = true;  
  25.     }  
  26.     //限定只能输入两位小数   
  27.     if (e.KeyChar != '\b' && (((TextBox)sender).SelectionStart) > (((TextBox)sender).Text.LastIndexOf('.')) + 2 && ((TextBox)sender).Text.IndexOf(".") >= 0)  
  28.         e.Handled = true;  
  29.     //光标在小数点右侧时判断输入是否合规  
  30.     if (e.KeyChar != '\b' && ((TextBox)sender).SelectionStart >= (((TextBox)sender).Text.LastIndexOf('.')) && ((TextBox)sender).Text.IndexOf(".") >= 0)  
  31.     {  
  32.        //光标位于小数点右侧第一位时判断  
  33.                 if ((((TextBox)sender).SelectionStart) == (((TextBox)sender).Text.LastIndexOf('.')) + 1)  
  34.                 {  
  35.                     if ((((TextBox)sender).Text.Length).ToString() == (((TextBox)sender).Text.IndexOf(".") + 3).ToString())  
  36.                         e.Handled = true;  
  37.                 }  
  38.       //光标位于小数点右侧第一位时判断  
  39.                 if ((((TextBox)sender).SelectionStart) == (((TextBox)sender).Text.LastIndexOf('.')) + 2) 
  40.                 { if ((((TextBox)sender).Text.Length - 3).ToString() == ((TextBox)sender).Text.IndexOf(".").ToString())                                e.Handled = true; 
  41.                 } 
  42.     } 
  43. }


2.VB.net 代码

</>code

  1. Private Sub TextBox1_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress  
  2.   
  3.        e.Handled = True  
  4.        '输入0-9和回连键时有效  
  5.        If (e.KeyChar >= "0" And e.KeyChar <= "9") Or e.KeyChar = "" Then  
  6.            e.Handled = False  
  7.        End If  
  8.        '输入小数点情况  
  9.        If e.KeyChar = "." Then  
  10.            '小数点不能在第一位  
  11.            If TextBox1.Text.Length <= 0 Then  
  12.                e.Handled = True  
  13.            Else  
  14.                '小数点不在第一位  
  15.                Dim f As Double  
  16.                If Double.TryParse(TextBox1.Text + e.KeyChar, f) Then  
  17.                    e.Handled = False  
  18.                End If  
  19.            End If  
  20.        End If  
  21.    End Sub



如对本文有疑问,请提交到交流论坛,广大热心网友会为你解答!! 点击进入论坛

发表评论 (2075人查看0条评论)
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
昵称:
最新评论
------分隔线----------------------------

快速入口

· 365软件
· 杰创官网
· 建站工具
· 网站大全

其它栏目

· 建站教程
· 365学习

业务咨询

· 技术支持
· 服务时间:9:00-18:00
365建站网二维码

Powered by 365建站网 RSS地图 HTML地图

copyright © 2013-2024 版权所有 鄂ICP备17013400号