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-Winform TextBox中只能输入数字的几种常用方法(C#)的更多相关文章

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

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

  2. C#-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. WinForm的TextBox限制只能输入数字并且屏蔽默认右键菜单

    基于Window消息实现 class TextBoxExt:TextBox { private const int WM_RBUTTONDOWN = 0x0204; private const int ...

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

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

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

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

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

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

随机推荐

  1. Android Hack1 使用weight属性实现视图的居中显示

    本文地址:http://www.cnblogs.com/wuyudong/p/5898403.html,转载请注明源地址. 如果要实现如下图所示的将按钮居中显示,并且占据父视图的一半,无论屏幕是否旋转 ...

  2. python之(re)正则表达式下

    知识储备: \w 匹配任何字母/数字,下划线 正则表达式进阶: re.match('com', 'comwww.runcomoob') re.search('\dcom', 'www.4comruno ...

  3. 解决Jenkins 2.0 初始化界面卡住的问题

    ***************************************** *原创博客转载请注明出处,谢谢!* **************************************** ...

  4. SQL Server Reporting Services:无法检索应用程序文件。部署中的文件已损坏

    如果在客户端计算机上启动Microsoft SQL Server 2012的 ClickOnce 版本的 Microsoft SQL Server 报表生成器时出现"无法检索应用程序文件.部 ...

  5. SQL SERVER 2000 迁移后SQL SERVER代理服务启动错误分析

    公司有一个老系统,这个系统所用的数据库是SQL SERVER 2000,它所在的Dell服务器已经运行超过10年了,早已经过了保修服务期,最近几乎每周会出现一次故障,加之5月份另外一台服务器坏了两个硬 ...

  6. C# Combobox 设置 value

    因为ComboxItem是Object对象,而控件自身没有Value属性.所以,需要自定义一个类,用其对象来存储Text,Value. public class ComboxItem    {     ...

  7. 从零自学Hadoop(11):Hadoop命令上

    阅读目录 序 概述 Hadoop Common Commands User Commands Administration Commands File System Shell 引用 系列索引 本文版 ...

  8. 一则因为numa引发的mysqldump hang住

    新买的dell r430服务器,双CPU,64G内存,单CPU32g,swap 3G 出现故障现像:mysqldump时会hang住,innodb_buffer_pool_size        = ...

  9. Asp.Net MVC+BootStrap+EF6.0实现简单的用户角色权限管理5

    我们先直接拷贝下blank.html这个页面的代码,顺带先建立一个Home控制器,并添加Index视图.将代码拷贝进去. <!DOCTYPE html> <html lang=&qu ...

  10. Migrate Instance 操作详解 - 每天5分钟玩转 OpenStack(40)

    Migrate 操作的作用是将 instance 从当前的计算节点迁移到其他节点上. Migrate 不要求源和目标节点必须共享存储,当然共享存储也是可以的. Migrate 前必须满足一个条件:计算 ...