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. {
  48. const string pattern = @"^\d+\.?\d+{1}quot;;
  49. string content = ((TextBox)sender).Text;
  50. if (!(Regex.IsMatch(content, pattern)))
  51. {
  52. errorProvider1.SetError((Control)sender, "只能输入数字!");
  53. e.Cancel = true;
  54. }
  55. else
  56. errorProvider1.SetError((Control)sender, null);
  57. }
  58. 方法五:
  59. private void textBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
  60. {
  61. if(e.KeyChar=='.' && this.textBox1.Text.IndexOf(".")!=-1)
  62. {
  63. e.Handled=true;
  64. }
  65. if(!((e.KeyChar>=48 && e.KeyChar<=57) || e.KeyChar=='.' || e.KeyChar==8))
  66. {
  67. e.Handled=true;
  68. }
  69. }
  70. 方法六:
  71. private void tbx_LsRegCapital_KeyPress(object sender, KeyPressEventArgs e)
  72. {
  73. if (!Char.IsNumber(e.KeyChar) && !Char.IsPunctuation(e.KeyChar) && !Char.IsControl(e.KeyChar))
  74. {
  75. e.Handled = true;//消除不合适字符
  76. }
  77. else if (Char.IsPunctuation(e.KeyChar))
  78. {
  79. if (e.KeyChar != '.' || this.textBox1.Text.Length == 0)//小数点
  80. {
  81. e.Handled = true;
  82. }
  83. if (textBox1.Text.LastIndexOf('.') != -1)
  84. {
  85. e.Handled = true;
  86. }
  87. }
  88. }
  89. 方法七:
  90. 利用ASCII码处理办法、
  91. {
  92. if ((e.KeyChar <= 48 || e.KeyChar >=57) && (e.KeyChar != 8) && (e.KeyChar != 46))
  93. e.Handled = true;
  94. ================48代表0,57代表9,8代表空格,46代表小数点
  95. }

C#-WinForm-TextBox中只能输入数字的几种常用方法(C#)的更多相关文章

  1. Winform TextBox中只能输入数字的几种常用方法(C#)

    方法一: private void tBox_KeyPress(object sender, KeyPressEventArgs e) { ; //禁止空格键 )) return; //处理负数 if ...

  2. C#-WinForm-Winform TextBox中只能输入数字的几种常用方法(C#)

    方法一: private void tBox_KeyPress(object sender, KeyPressEventArgs e) { if (e.KeyChar == 0x20) e.KeyCh ...

  3. C#的winform中控制TextBox中只能输入数字

    C#的winform中控制TextBox中只能输入数字 private void textBox3_KeyPress(object sender, System.Windows.Forms.KeyPr ...

  4. winform中如何在TextBox中只能输入数字(可以带小数点)

    可以采用像web表单验证的方式,利用textbox的TextChanged事件,每当textbox内容变化时,调用正则表达式的方法验证,用一个label在text后面提示输入错误,具体代码如下: pr ...

  5. 限定textbox中只能输入数字的小方法

    在textbox中加入onkeyup="this.value=this.value.replace(/\D/g,' ')"即可实现这一功能 验证数字的正则表达式:^[0-9]*$或 ...

  6. 控制input标签中只能输入数字以及小数点后两位

    js 代码如下: /* 控制input标签中只能输入数字 和小数点后两位 */ function checkNum(obj) { //检查是否是非数字值 if (isNaN(obj.value)) { ...

  7. .net(c#) winform文本框只能输入数字,不能其他非法字符

    private void textBox3_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e) { //阻止从键盘输入键 ...

  8. input输入框中只能输入数字,非数字字符自动清除

    前言:项目中有个缴纳保证金的功能,要是输入框只能输入数字,不能输入其他字符. ①HTML代码:<input class="input-box" type="text ...

  9. 关于input只能输入数字的两种小方法

    第一种: 直接给input标签 name赋值如下 <input name="start_price" id="start_price" type=&quo ...

随机推荐

  1. FIX protocol tutorial : Fix Session is not connecting how to diagnose it ?

    In this blog post of FIX protocol tutorial series I would like to share my experience with connectiv ...

  2. 运行Junit单测时遇到的问题

    现在有两个办法解决: 1.junit版本降到4.10 2.导入hamcrest-core-1.3.jar 官网:JUnit now uses the latest version of Hamcres ...

  3. 特性(property)

    6.4 特性(property) 1 什么是特性property property是一种特殊的属性,访问它时会执行一段功能(函数)然后返回值 import math class Circle: def ...

  4. 全球数据库-->基金/管理产品-->基金分析/新闻/报告

    加拿大共同基金 澳大利亚投资信托 美国ETF 美国共同基金 英国投资信托基金 名称 分析师名称 分析日期 晨星分析师评级 晨星简报

  5. TableLayout 里的TextView等组的LayoutParams参数问题

    TableLayout 里的TextView等组的LayoutParams参数不能是LinearLayout.LayoutParams这样来定义, 只能是用TableRow.LayoutParams ...

  6. Uploadify多文件上传插件.NET使用案例+PHP使用案例

    ploadify是一个非常好用的多文件上传插件 插件下载:http://www.uploadify.com 下载后需要用到的文件: 接下来就是直接添加代码: Default.aspx代码 <%@ ...

  7. 如何讓 iOS UIWebView 連線時傳送自訂 Cookie 的方法[转]

    利用 NSHTTPCookieStorage 管理 Cookie 傳送 在 iOS 中如果自行建立 UIWebView 來開啟遠端站台資料,這時可以透過以下方法加入 Cookie.原理是透過 iOS ...

  8. swift 自定义弹框

    // //  ViewController.swift //  animationAlert // //  Created by su on 15/12/9. //  Copyright © 2015 ...

  9. 树莓派3下安装TL-WN722N无线网卡驱动

    最近在搞树莓派,我的是国产的树莓派3卡片电脑,想整成一个无线加有线的路由器(树莓派3.TL-WN722N无线网卡.集线器)或者是搭个web认证的WIFI钓鱼热点玩玩. 一开始就遇到问题了,连无线网卡的 ...

  10. IOC容器基本原理

    1  IoC容器的概念 IoC容器就是具有依赖注入功能的容器,IoC容器负责实例化.定位.配置应用程序中的对象及建立这些对象间的依赖.应用程序无需直接在代码中new相关的对象,应用程序由IoC容器进行 ...