//选择文本框的事件窗口,找到按键输入的方法KeyPress,双击建立新的方法。
/// <summary>
/// textBox只能输入数字的处理方法
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void textBox6_KeyPress(object sender, KeyPressEventArgs e)
{
//判断按键是不是要输入的类型。
var textBox1 = (TextBox)sender;
if (((int)e.KeyChar < 48 || (int)e.KeyChar > 57) && (int)e.KeyChar != 8 && (int)e.KeyChar != 46 && e.KeyChar != 0x2D)
e.Handled = true;
//处理负数
if ((e.KeyChar == 0x2D) && (((TextBox)sender).Text.Length != 0)) e.Handled = true;
//处理0(如果第一位为0且不是全选的情况的话只能输入小数点或者退格键)
if ((int)e.KeyChar != 46 && e.KeyChar != '\b' && textBox1.SelectionLength != textBox1.TextLength)
{
//分正负数两种情况
if (textBox1.TextLength == 1 && textBox1.Text.Substring(0, 1) == "0")
{
e.Handled = true;
}
else if (textBox1.TextLength == 2 && textBox1.Text.Substring(0) == "-0")
{
e.Handled = true;
}
} //小数点的处理。
if ((int)e.KeyChar == 46) //小数点
{
if (textBox1.Text.Length <= 0 || textBox1.Text.LastIndexOf('.') != -1)
e.Handled = true; //小数点不能在第一位(正数)或不能有多个小数点
else if(textBox1.TextLength==1 && textBox1.Text.Substring(0) == "-")
{
e.Handled = true; //小数点不能在第一位(负数)
}
}
}
输入值是数字(含小数)
     /// <summary>
/// 判断输入值是否符合要求(含小数)
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
public static void tbx_LsRegCapital_KeyPress(object sender, KeyPressEventArgs e)
{
var textBox1 = (TextBox)sender;
if (e.KeyChar == 0x20) e.KeyChar = (char)0; //禁止空格键
if ((e.KeyChar == 0x2D) && (((TextBox)sender).Text.Length == 0)) return; //处理负数
//处理0(如果第一位为0且不是全选的情况的话只能输入小数点或者退格键)
if ((int)e.KeyChar != 46 && e.KeyChar != '\b' && textBox1.SelectionLength != textBox1.TextLength)
{
//分正负数两种情况
if (textBox1.TextLength == 1 && textBox1.Text.Substring(0, 1) == "0")
{
e.Handled = true;
}
else if (textBox1.TextLength == 2 && textBox1.Text.Substring(0) == "-0")
{
e.Handled = true;
}
}
if (e.KeyChar > 0x20)
{
try
{
double.Parse(((TextBox)sender).Text + e.KeyChar.ToString());
}
catch
{
e.KeyChar = (char)0; //处理非法字符
}
}
}
正整数
     /// <summary>
/// 判断输入值是否符合要求(正整数)
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
public static void tbx_Int_KeyPress(object sender, KeyPressEventArgs e)
{
var textBox1 = (TextBox)sender;
if (e.KeyChar == 0x20 || e.KeyChar == 0x2D || (int)e.KeyChar == 46) e.KeyChar = (char)0; //禁止空格键和负数/小数
if (
((textBox1.TextLength == 0 || textBox1.TextLength==textBox1.SelectionLength) && (int)e.KeyChar == 48)
|| (textBox1.TextLength == 1 && textBox1.Text=="0" && textBox1.TextLength != textBox1.SelectionLength)
)
e.Handled = true;//0的处理
if (e.KeyChar > 0x20)
{
try
{
double.Parse(((TextBox)sender).Text + e.KeyChar.ToString());
}
catch
{
e.KeyChar = (char)0; //处理非法字符
}
}
}
48代表0,57代表9,8代表空格,46代表小数点 ,0x2D代表负数, '\b'代表退格键
第一种代码考虑了各种情况,如不需要可以去除部分代码实现更简单的效果,例如如果不需要负数的就可以把所有关于负数判断的去掉,灵活运用

c# TextBox只能输入数字的处理方法(完整版各种情况考虑在内,可根据需求灵活修改)的更多相关文章

  1. Asp.net TextBox只能输入数字

    原文:Asp.net TextBox只能输入数字 <asp:textbox id="TextBox1" onkeyup="if(isNaN(value))execC ...

  2. TextBox只能输入数字

    Asp.net TextBox只能输入数字 <asp:textbox id="TextBox1" onkeyup="if(isNaN(value))execComm ...

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

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

  4. C#设置textBox只能输入数字(正数,负数,小数)简单实现

    /* *设置textBox只能输入数字(正数,负数,小数) */ public static bool NumberDotTextbox_KeyPress(object sender, KeyPres ...

  5. [WinForm]TextBox只能输入数字或者正浮点型数字

    关键代码: /// <summary> /// 只能输入数字[KeyPress事件] /// </summary> /// <param name="textB ...

  6. winform 中TextBox只能输入数字

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

  7. textbox只能输入数字或中文的常用正则表达式和验证方法

    验证数字的正则表达式集 验证数字:^[0-9]*$ 验证n位的数字:^\d{n}$ 验证至少n位数字:^\d{n,}$ 验证m-n位的数字:^\d{m,n}$ 验证零和非零开头的数字:^(0|[1-9 ...

  8. C# TextBox 只能输入数字

    private void textBox1_KeyPress(object sender, KeyPressEventArgs e) { TextBox txt = sender as TextBox ...

  9. UWP textbox 只能输入数字

    private void Testbox_TextChanged(object sender, TextChangedEventArgs e) {    var textbox = (TextBox) ...

随机推荐

  1. 日志模块详细介绍 hashlib模块 动态加盐

    目录 一:hashlib模块 二:logging 一:hashlib模块 加密: 将明文数据通过一系列算法变成密文数据(目的就是为了数据的安全) 能够做文件一系列校验 python的hashlib提供 ...

  2. vi与vim编辑器与解决vim编辑异常

    目录 一:vi与vim编辑器 二:解决vim编辑异常 一:vi与vim编辑器 vim是vi的升级版编辑器,就是vim比vi丰富一些. 1.安装vim 命令 yum install vim -y 2.打 ...

  3. AWS SAA_C01 考试分享。

    Saa-c01 经验分享! 序言1.介绍自己的情况,我是一个做后台开发的初级java程序员.还是处于在写业务逻辑的阶段,我对aws可谓是啥都不懂,纯种的小白,完全是从0基础开始学习的.希望分享一些我的 ...

  4. ApacheCN Vue 译文集 20211115 更新

    使用 GraphQL 构建 VueJS 应用 零.前言 一.数据绑定.事件和计算属性 二.组件.混合器和功能组件 三.设置我们的聊天应用--AWS Amplify 环境和 GraphQL 四.创建自定 ...

  5. Net6 DI源码分析Part1 ServiceCollection、ServiceDescriptor、ServiceLifetime、IServiceProvider

    ServiceCollection.ServiceDescriptor.ServiceLifetime.IServiceProvider Microsoft.Extensions.Dependency ...

  6. DESUtil

    package com.tebon.ams.util;import sun.misc.BASE64Decoder;import javax.crypto.Cipher;import javax.cry ...

  7. 【HTML】table表格拆分合并(colspan、rowspan)

    代码演示 横向合并: <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http:// ...

  8. docker 网络概述及网络模式详解

    docker 网络概述及网络模式详解 1.网络概述 2.网络模式详解 1.网络概述: Docker 网络实现原理 Docker使用Linux桥接,在宿主机虚拟一个Docker容器网桥(docker0) ...

  9. python——平时遇到问题记录

    # pyhs2安装 #centos yum install groupinstall 'development tools' yum install python34-devel yum instal ...

  10. Spring 初始化流程

    开始 在SpringIOC中,前面讲述了如何配置BeanDefinition和如何注册BeanDefinition,但是这些知识容器初始化的一部分,在AbstractApplicationContex ...