在Winform(C#)中要实现限制Textbox只能输入数字,一般的做法就是在按键事件中处理,判断keychar的值。限制只能输入数字,小数点,Backspace,del这几个键。数字0~9所对应的keychar为48~57,小数点是46,Backspace是8。    
      拖一个Textbox到窗体上,添加OnKeyPress事件,在事件写判断的代码,只要判断不是这些键,设置e.Handled的值为true,就可以屏蔽输入。

方法一:

private void tBox_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == 0x20) e.KeyChar = (char); //禁止空格键
if ((e.KeyChar == 0x2D) && (((TextBox)sender).Text.Length == )) return; //处理负数
if (e.KeyChar > 0x20)
{
try
{
double.Parse(((TextBox)sender).Text + e.KeyChar.ToString());
}
catch
{
e.KeyChar = (char); //处理非法字符
}
}
}

方法二:

private void TextBox_KeyPress(object sender, KeyPressEventArgs e)
{
if(e.KeyChar!=&&!Char.IsDigit(e.KeyChar))
{
e.Handled = true;
}
}

或者

private void TextBox_KeyPress(object sender, KeyPressEventArgs e)
{
if(e.KeyChar!='\b'&&!Char.IsDigit(e.KeyChar))
{
e.Handled = true;
} }

方法三:

private void textBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
if(e.KeyChar!='\b')//这是允许输入退格键
{
if((e.KeyChar<'')||(e.KeyChar>''))//这是允许输入0-9数字
{
e.Handled = true;
}
}
}

方法四:

private void textBox1_Validating(object sender, CancelEventArgs e)
{
const string pattern = @"^\d+\.?\d+$";
string content = ((TextBox)sender).Text; if (!(Regex.IsMatch(content, pattern)))
{
errorProvider1.SetError((Control)sender, "只能输入数字!");
e.Cancel = true;
}
else
errorProvider1.SetError((Control)sender, null);
}

方法五:

private void textBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
if(e.KeyChar=='.' && this.textBox1.Text.IndexOf(".")!=-)
{
e.Handled=true;
} if(!((e.KeyChar>= && e.KeyChar<=) || e.KeyChar=='.' || e.KeyChar==))
{
e.Handled=true;
} }

方法六:

private void tbx_LsRegCapital_KeyPress(object sender, KeyPressEventArgs e)
{
if (!Char.IsNumber(e.KeyChar) && !Char.IsPunctuation(e.KeyChar) && !Char.IsControl(e.KeyChar))
{
e.Handled = true;//消除不合适字符
}
else if (Char.IsPunctuation(e.KeyChar))
{
if (e.KeyChar != '.' || this.textBox1.Text.Length == )//小数点
{
e.Handled = true;
}
if (textBox1.Text.LastIndexOf('.') != -)
{
e.Handled = true;
}
}
}

方法七:

利用ASCII码处理办法、
{

if ((e.KeyChar <= 48 || e.KeyChar >=57) && (e.KeyChar != 8) && (e.KeyChar != 46))
              e.Handled = true;
================48代表0,57代表9,8代表空格,46代表小数点
}

WinForm 中限制只能输入数字的更多相关文章

  1. winform 中TextBox只能输入数字

    textBox1.KeyPress+=TextNumber_KeyPress; private void TextNumber_KeyPress(object sender, KeyPressEven ...

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

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

  3. asp.net中TextBox只能输入数字的最简洁的两种方法

    如下TextBox <asp:textboxonkeypress="isnum()"id="TextBox1"runat="server&quo ...

  4. ASP.NET 中Textbox只能输入数字,不能输入其他字符

    解决办法如下:  <asp:TextBox ID="txtpage" runat="server" Width="61px"     ...

  5. vue中输入框只能输入数字

    方案1:增加自定义指令 自定义指令写法:      directives: {         numberOnly: {             bind(el) {                 ...

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

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

  7. C# textbox中输入时加限制条件 // C#Winform下限制TextBox只能输入数字 // 才疏学浅(TextBox 小数点不能在首位+只能输入数字)

    textbox中输入时加限制条件 分类: C# winform2008-08-26 08:30 306人阅读 评论(0) 收藏 举报 textbox正则表达式object 1.用正则表达式! 2.使用 ...

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

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

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

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

随机推荐

  1. [译]libcurl错误码

    CURLcode Almost all "easy" interface functions return a CURLcode error code. No matter wha ...

  2. EasyUI系列学习(六)-Tooltip(提示框)

    一.创建组件 0.Tooltip不依赖其他组件 1.使用class加载 <a href="#" class="easyui-tooltip" title= ...

  3. 利用freemarker导出页面格式复杂的excel

    刚开始大家可能会利用poi生成简单的excel,但是遇到需要生成复杂的excel,poi导出excel就比较困难,这时候可以利用freemarker来渲染实现实现生成复杂的excel, 首先,将exc ...

  4. 实现div左右上下都居中

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  5. switch-case用法

    1.switch-case 一般的用它来做值匹配的. //匹配 就是全等. /* 语法: switch(表达式){ case 值1: 表达式的值和 值1匹配上了,需要执行的代码; break; cas ...

  6. WordPress极简主题Small Cat详细介绍

    主题特性: HTML5.CSS3 使用标准语言编写,支持IE10以上浏览器 响应式 在桌面.平板.手机端均以最佳状态显示.也可分享到微信显示哦! 自定义 超过60多个后台自定义设置,让你的站点与众不同 ...

  7. PHP7中session_start 使用注意事项,会导致浏览器刷时页面数据不更新

    //PHP7中session_start 使用注意事项, session_start([ 'cache_limiter' => 'private', //在读取完毕会话数据之后马上关闭会话存储文 ...

  8. 场景分割:MIT Scene Parsing 与DilatedNet 扩展卷积网络

    MIT Scene Parsing Benchmark简介 Scene parsing is to segment and parse an image into different image re ...

  9. swift Hashable Equatable

    /// You can use any type that conforms to the `Hashable` protocol in a set or /// as a dictionary ke ...

  10. 搭建FileZilla

    FileZilla是C/S架构的,有服务端和客户端 客户端下载地址https://www.filezilla.cn/download/client 安装,一般就下一步下一步了. 服务端下载:https ...