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. Golang(笔记) 面向对象

    package main import ( "fmt" ) //对象定义 type Rect struct{ x,y float64 width ,height float64 } ...

  2. iOS cocoapods升级及问题

    安装 安装RubyCocoaPods基于Ruby语言开发而成,因此安装CocoaPods前需要安装Ruby环境.幸运的是Mac系统默认自带Ruby环境,如果没有请自行查找安装.检测是否安装Ruby:$ ...

  3. IOS开发基础知识--碎片48

    1:Assertion failure in dequeueReusableCellWithIdentifier:forIndexPath:  static NSString *CellIdentif ...

  4. 浅谈RecyclerView(完美替代ListView,GridView)

    Android RecyclerView 是Android5.0推出来的,导入support-v7包即可使用. 个人体验来说,RecyclerView绝对是一款功能强大的控件. 首先总结下Recycl ...

  5. iOS--xuer(registration)

    这个登录页面包含了自适应屏幕的大小,数字用户登录键盘是数字键盘.隐藏键盘.隐藏密码等等. ViewController.h #import <UIKit/UIKit.h> #import ...

  6. Orchard中如何配置远端发布

    Orchard中默认安装是有Blog功能的.下面介绍如何配置Remote Blog Publishing功能,使用Windows Live Writer客户端发布博客. 一,开启Remote Blog ...

  7. ORA-02292: integrity constraint (xxxx) violated - child record found

    在更新表的主键字段或DELETE数据时,如果遇到ORA-02292: integrity constraint (xxxx) violated - child record found 这个是因为主外 ...

  8. goldengate abended with no data found

    先来看下报错ggserr.log: 2016-12-22 04:48:52  WARNING OGG-02544  Unhandled error (ORA-26787: The row with k ...

  9. union和union all用法

    工作中,遇到同事之前写的oracle语句中有一个union all,并且很多地方都用到了.便在网上查了一下用法,以下是自己的理解. union  (联合)将两个或者多个结果集合并. 在使用时,两个结果 ...

  10. 【转】【51CTO 网+】怎样做一款让用户来电的产品

    [51CTO 网+]怎样做一款让用户来电的产品 据相关调查显示,目前全球移动用户平均每人安装应用约95个,每天使用的应用约35个.可见面对众多的移动应用,用户拥有非常大的选择空间.如果由于交互设计欠佳 ...