iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > 其他教程 >C#中如何限制TextBox控件内输入值的范围
  • 593
分享到

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

C#TextBox控件TextBox控件输入值C#TextBox控件输入值 2023-01-28 06:01:44 593人浏览 安东尼
摘要

目录C#限制TextBox控件内输入值的范围举个例子C#TextBox控件限制只允许输入数字及小数点处理只输入数字的C# 文本框只能输入数字和退格键总结C#限制TextBox控件内输

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

举个例子

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

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

代码如下:

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事件

代码如下:

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控件限制只允许输入数字及小数点

//判断按键是不是要输入的类型。
 
            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代表小数点 
}

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

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;
   }
  }

判断是否为空

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

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程网。

--结束END--

本文标题: C#中如何限制TextBox控件内输入值的范围

本文链接: https://www.lsjlt.com/news/178347.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作