服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C# - C#中如何限制TextBox控件内输入值的范围

C#中如何限制TextBox控件内输入值的范围

2023-03-01 15:18Danny_hi C#

这篇文章主要介绍了C#中如何限制TextBox控件内输入值的范围,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

C#限制TextBox控件内输入值的范围

举个例子

比如要限制TextBox1控件内只能输入1~100的数字(先将TextBox1的MaxLength属性设置成3):

1.首先要限制输入的只能是数值,不能是字母或其他符号;选择添加textBox1的KeyPress事件

代码如下:

?
1
2
3
4
5
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!(Char.IsNumber(e.KeyChar)) && e.KeyChar != (char)8)
                e.Handled = true;
        }

2.再限制输入数值的范围1~100;选择添加textBox1的TextChanged事件

代码如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
private void textBox1_TextChanged(object sender, EventArgs e)
        {
            if (textBox1.Text == ""
                textBox1.Text = 0.ToString(); 
            int number = int.Parse(textBox1.Text);
            textBox1.Text = number.ToString();
            if (number <= 100)
            {
                return;
            }
            textBox1.Text = textBox1.Text.Remove(2);
            textBox1.SelectionStart = textBox1.Text.Length;
        }

C#TextBox控件限制只允许输入数字及小数点

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
//判断按键是不是要输入的类型。
 
            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;
 
                    }
 
                }
 
            }

处理只输入数字的

方法一:    

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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;   //处理非法字符 
               
           

方法二:    

?
1
2
3
4
5
6
7
private void TextBox_KeyPress(object sender, KeyPressEventArgs e) 
 
    if(e.KeyChar!=8&&!Char.IsDigit(e.KeyChar)) 
   
      e.Handled = true
   

或者    

?
1
2
3
4
5
6
7
8
private void TextBox_KeyPress(object sender, KeyPressEventArgs e) 
    if(e.KeyChar!='\b'&&!Char.IsDigit(e.KeyChar)) 
   
      e.Handled = true
   
   

方法三:    

?
1
2
3
4
5
6
7
8
9
10
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

方法四:    

?
1
2
3
4
5
6
7
8
9
10
11
12
13
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);  

方法五:    

?
1
2
3
4
5
6
7
8
9
10
11
12
13
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
   

方法六:    

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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码处理办法、

?
1
2
3
4
5
6
   
            if ((e.KeyChar <= 48 || e.KeyChar >=57) && (e.KeyChar != 8) && (e.KeyChar != 46)) 
              e.Handled = true
================48代表0,57代表9,8代表空格,46代表小数点 
}

C# 文本框只能输入数字和退格键

?
1
2
3
4
5
6
7
private void TextBox_KeyPress(object sender, KeyPressEventArgs e)
  {
   if(e.KeyChar!=8&&!Char.IsDigit(e.KeyChar))
   {
     e.Handled = true;
   }
  }

或者

?
1
2
3
4
5
6
7
private void TextBox_KeyPress(object sender, KeyPressEventArgs e)
  {
   if(e.KeyChar!='\b'&&!Char.IsDigit(e.KeyChar))
   {
     e.Handled = true;
   }
  }

判断是否为空

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

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/qq_43024228/article/details/86604365

延伸 · 阅读

精彩推荐
  • C#C# WinForm打开PDF文件并在窗体中显示

    C# WinForm打开PDF文件并在窗体中显示

    本文主要介绍通过引用Adobe reader提供的COM组件,以实现在WinForm程序中显示PDF文件的功能。...

    wenjunsu7902021-11-21
  • C#详细分析c# 客户端内存优化

    详细分析c# 客户端内存优化

    这篇文章主要介绍了c# 客户端内存优化的相关资料,文中示例代码非常详细,帮助大家更好的理解和学习,感兴趣的朋友可以了解下...

    Jlion7772022-09-22
  • C#Unity实现图形相交检测

    Unity实现图形相交检测

    这篇文章主要为大家详细介绍了Unity实现图形相交检测,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    小混沌11672022-09-05
  • C#WinForm中Application.Idle方法详解

    WinForm中Application.Idle方法详解

    本文详细讲解了WinForm中的Application.Idle方法,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下...

    .NET开发菜鸟8172023-02-16
  • C#Unity实现透视滑动列表

    Unity实现透视滑动列表

    这篇文章主要为大家详细介绍了Unity实现透视滑动列表,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    咸鱼永不翻身5652022-11-28
  • C#快速了解c# 结构体

    快速了解c# 结构体

    这篇文章主要介绍了c# 结构体的相关资料,文中示例代码非常详细,帮助大家更好的理解和学习,感兴趣的朋友可以了解下...

    少年。4482022-09-21
  • C#C#交错数组知识点分析

    C#交错数组知识点分析

    在本篇文章里小编给大家整理的是关于C#交错数组知识点分析,需要的朋友们参考下。...

    ONEYF8432022-08-10
  • C#C# 使用AE获取feature的属性及字段操作

    C# 使用AE获取feature的属性及字段操作

    这篇文章主要介绍了C# 使用AE获取feature的属性及字段操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...

    南方北先生9752022-10-27