c#textBox控件限制只允许输入数字及小数点 转载

//判断按键是不是要输入的类型。

            if (((int)e.KeyChar < 48 || (int)e.KeyChar > 57) && (int)e.KeyChar != 8 && (int)e.KeyChar != 46)

                e.Handled = true;

            //小数点的处理。

            if ((int)e.KeyChar == 46)                           //小数点

            {

                if (textBox1.Text.Length <= 0)

                    e.Handled = true;   //小数点不能在第一位

                else

                {

                    float f;

                    float oldf;

                    bool b1 = false, b2 = false;

                    b1 = float.TryParse(textBox1.Text, out oldf);

                    b2 = float.TryParse(textBox1.Text + e.KeyChar.ToString(), out f);

                    if (b2 == false)

                    {

                        if (b1 == true)

                            e.Handled = true;

                        else

                            e.Handled = false;

                    }

                }

            }

   处理只输入数字的:

方法一:  

private void tBox_KeyPress(object sender, KeyPressEventArgs e)  

 {
if (e.KeyChar == 0x20) e.KeyChar = (char)0; //禁止空格键
if ((e.KeyChar == 0x2D) && (((TextBox)sender).Text.Length == 0)) return; //处理负数
if (e.KeyChar > 0x20)
{
try
{
double.Parse(((TextBox)sender).Text + e.KeyChar.ToString());
}
catch
{
e.KeyChar = (char)0; //处理非法字符
}
}
} 方法二: private void TextBox_KeyPress(object sender, KeyPressEventArgs e)
{
if(e.KeyChar!=8&&!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<'0')||(e.KeyChar>'9'))//这是允许输入0-9数字
{
e.Handled = true;
}
}
} 方法四: private void textBox1_Validating(object sender, CancelEventArgs e)
{
const string pattern = @"^\d+\.?\d+{1}quot;;
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(".")!=-1)
{
e.Handled=true;
} if(!((e.KeyChar>=48 && e.KeyChar<=57) || e.KeyChar=='.' || e.KeyChar==8))
{
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 == 0)//小数点
{
e.Handled = true;
}
if (textBox1.Text.LastIndexOf('.') != -1)
{
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代表小数点
}

 

判断是否为空

 if (string.IsNullOrWhiteSpace(txtDir.Text))//指示指定的字符串是 null、空还是仅由空白字符组成。

 

c#textBox控件限制只允许输入数字及小数点,是否为空的更多相关文章

  1. c#textBox控件限制只允许输入数字及小数点

    在textboxd的事件中的 KeyPress 事件,这样双击进入代码:输入以下代码 即可 //判断按键是不是要输入的类型. || () && ( && () e.Ha ...

  2. C#中设置TextBox控件中仅可以输入数字且设置上限

    首先设置只可以输入数字: 首先设置TextBox控件的KeyPress事件:当用户按下的键盘的键不在数字位的话,就禁止输入 private void textBox1_KeyPress(object ...

  3. ASP.NET c# textbox 正则表达式 文本框只允许输入数字(验证控件RegularExpressionValidator )

    <input type="text" name="test" onKeyUp="test1.value=(this.value=this.val ...

  4. TextBox控件保存上次的输入

    本片文章是参考C# 怎么让winform程序中的输入文本框保留上次的输入再此表示感谢重新在这里写一遍,是为了保存一下,方便自己下次使用可以很快的找到1.设置txtBox控件的配置文件2.选择Text ...

  5. contextField 键盘只允许输入数字和小数点,并且现在小数点后位数

    - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementS ...

  6. C# textBox控件只允许为数字和小数点并且提取出这个数字

    一. textBox控件实现只允许为数字和小数点 如下图所示,在textBox控件框内输入只能是 要在textBox控件属性设置按键按下的事件触发,如下图所示: 二.源代码 textBox控件只允许为 ...

  7. 只允许输入数字的TextBox控件

    [实例说明] 可以在TextBox控件中轻松地输入文本信息,输入的文本信息可以包括字母.数字.汉字等. 如果需要用户在TextBox控件中填写年龄信息,那么年龄信息应当只允许数字,怎么限制用户输入其他 ...

  8. c# TextBox只允许输入数字,禁用右键粘贴,允许Ctrl+v粘贴数字

    TextBox只允许输入数字,最大长度为10 //TextBox.ShortcutsEnabled为false 禁止右键和Ctrl+v private void txtNumber_KeyPress( ...

  9. 2019-3-22c# TextBox只允许输入数字,禁用右键粘贴,允许Ctrl+v粘贴数字

    TextBox 禁止复制粘贴 ShortcutsEnabled =false TextBox只允许输入数字,最大长度为10 //TextBox.ShortcutsEnabled为false 禁止右键和 ...

随机推荐

  1. pytorch max和clamp

    torch.max() torch.max(a):数组a的最大值 torch.max(a, dim=1):多维数组沿维度1方向上的最大值,若a为二维数组,则为每行的最大值(此时是对每行的每列值比较取最 ...

  2. 6.安装telnet服务

    yum list telnet* 列出telnet相关的安装包 yum install telnet-server 安装telnet服务 yum install telnet.* 安装telnet客户 ...

  3. leetcode817 Linked List Components

    """ We are given head, the head node of a linked list containing unique integer value ...

  4. HDU - 6201 transaction transaction transaction(spfa求最长路)

    题意:有n个点,n-1条边的无向图,已知每个点书的售价,以及在边上行走的路费,问任选两个点作为起点和终点,能获得的最大利益是多少. 分析: 1.从某个结点出发,首先需要在该结点a花费price[a]买 ...

  5. bzoj 3696: 化合物

    哦,这个困惑了我好久的东西——生成函数(母函数),(然而拿这个东西去向学文化课的同学装逼并不成功...) 生成函数,就是把原来的加法组合变成乘法的指数加法,那么我们要求的值就是相应的指数的系数的值啦, ...

  6. Oracle 序列(查询序列的值,修改序列的值)

    1.序列的语法形式 create sequence 序列名 increment by n start with n maxvalue n | nomaxvalue minvalue n | nomin ...

  7. 云时代架构阅读笔记一——Java性能优化(一)

    Java语言学习了这么长时间之后,自己对于Java编程的一些细节还是稍微有点总结,正好根据云时代架构中<Java高级开发必会的50个性能优化的细节(珍藏版)>来叙述一些我和里面的点比较相符 ...

  8. [转载]Jquery Chosen 插件动态生成option或重新绑定

    $(".chosen—select").find("option[value='1']").attr("selected", "s ...

  9. 九、SAP中使用定义时间及使用sy-uzeit取当前时间

    一.sy-uzeit为取当前时间函数,类型t为时间类型,代码如下: 二.输出结果如下:

  10. 110-PHP类成员属性赋值

    <?php class mao{ //定义猫类 public $age=0; //定义多个属性并初始化 public $weight=50; public $color='white'; } $ ...