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. Vue方法中修改数组某一项元素而不能响应式更新

    <template> <div> <ul> <li v-for="(item, i) in ms" :key="i"& ...

  2. Matplotlib 基本概念

    章节 Matplotlib 安装 Matplotlib 入门 Matplotlib 基本概念 Matplotlib 图形绘制 Matplotlib 多个图形 Matplotlib 其他类型图形 Mat ...

  3. 域名配置DNS解析A记录,映射到主机

    有很多域名的供应商,随便选,哪个便宜用哪个.godaddy一直支持支付宝,不用visa,虽然它是国外的. 我用的是godaddy,这两年有中文版的了,虽然它有了中文版,但是比以前的英文版还要慢. 进入 ...

  4. 51nod 1368:黑白棋 二分图最大匹配

    1368 黑白棋 题目来源: TopCoder 基准时间限制:1 秒 空间限制:131072 KB 分值: 160 难度:6级算法题  收藏  取消关注 有一个N*M的棋盘(1<=N,M< ...

  5. BubbleSort

    看见了一些乱乱的东西,就想着整理一下,基础的冒泡排序 //BubbleSort #include<iostream> using namespace std; void BubbleSor ...

  6. GoJS实例3

    复制如下内容保存到空白的.html文件中,用浏览器打开即可查看效果 <!DOCTYPE html> <html> <head> <meta charset=& ...

  7. 微信小程序—页面跳转

    问题: 实现页面跳转:index页面通过按钮跳转到next页面 方法: 1.页面index.wxml增加一个按钮 // index.wxml <button bindtap="jump ...

  8. 利用 vuex 实现一个公用搜索器

    安装 npm i vuex vuex 的使用 先创建好如图所示的文件: 编写 modules 下的 params.js const param = { state: { params: {} }, m ...

  9. 【剑指Offer】面试题28. 对称的二叉树

    题目 请实现一个函数,用来判断一棵二叉树是不是对称的.如果一棵二叉树和它的镜像一样,那么它是对称的. 例如,二叉树 [1,2,2,3,4,4,3] 是对称的.     1    / \   2   2 ...

  10. QT进行多传感器(执行器)的编程框架

    物联网的发展使得现今使用越来越广泛,对于多传感器进行管理变得十分有必要.使用传统的过程管理,很明显很容易陷入管理的混乱, 造成信息的不同步.使用面向对象的管理,以及对物理传感器在程序中进行抽象,并且建 ...