加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

C#中TextBox输入数字的几种常用方法

(2018-01-26 11:27:51)
标签:

it

分类: JAVA/C#/Cpp/C语言/Python
转载:C# TextBox中只能输入数字的几种常用方法(C#)

TextBox中只能输入数字的几种常用方法(C#)

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 button1_Click(object sender, EventArgs e) 

string text = this.textBox1.Text; 
if (text != null) 
MessageBox.Show(text); 


private void textBox1_Validating(object sender, CancelEventArgs e) 

const string pattern = @"^\d+\.?\d+$"; 
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代表小数点
}


================================================================

//设置textBox只能输入数字(正数,负数,小数)
public static bool NumberDotTextbox_KeyPress(object sender, KeyPressEventArgs e)
{
    //允许输入数字、小数点、删除键和负号
    if ((e.KeyChar < 48 || e.KeyChar > 57) && e.KeyChar != 8 && e.KeyChar != (char)('.') && e.KeyChar != (char)('-'))
    {
        return true;
    }
    if (e.KeyChar == (char)('-'))
    {
        if ((sender as TextBox).Text != "")
        {
            return true;
        }
    }
    //小数点只能输入一次
    if (e.KeyChar == (char)('.') && ((TextBox)sender).Text.IndexOf('.') != -1)
    {
        return true;
    }
    //第一位不能为小数点
    if (e.KeyChar == (char)('.') && ((TextBox)sender).Text == "")
    {
        return true;
    }
    //第一位是0,第二位必须为小数点
    if (e.KeyChar != (char)('.') && e.KeyChar != 8 && ((TextBox)sender).Text == "0")
    {
        return true;
    }
    //第一位是负号,第二位不能为小数点
    if (((TextBox)sender).Text == "-" && e.KeyChar == (char)('.'))
    {
        return true;
    }

    return false;
}

public static bool NumberTextbox_KeyPress(KeyPressEventArgs e)
{
    if (e.KeyChar != '\b')//这是允许输入退格键
    {
        if ((e.KeyChar < '0') || (e.KeyChar > '9'))//这是允许输入0-9数字
        {
            return true;
        }
    }

    return false;
}

===============================================================

首先设置只可以输入数字:

  首先设置TextBox控件的KeyPress事件:当用户按下的键盘的键不在数字位的话,就禁止输入


1    private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
2         {
3             if (e.KeyChar != 8 && !Char.IsDigit(e.KeyChar))//如果不是输入数字就不让输入
4             {
5                 e.Handled = true;
6             }
7         }

设置上限:

  设置TextBox的TextChanged事件如下


 1  private void textBox1_TextChanged(object sender, EventArgs e)
 2         {
 3             int iMax = 100;//首先设置上限值
 4             if (textBox1.Text != null && textBox1.Text != "")//判断TextBox的内容不为空,如果不判断会导致后面的非数字对比异常
 5             {
 6                 if (int.Parse(textBox1.Text) > iMax)//num就是传进来的值,如果大于上限(输入的值),那就强制为上限-1,或者就是上限值?
 7                 {
 8                     textBox1.Text = (iMax - 1).ToString();
 9                 }
10             }
11         }
================================================================

C#  winform中文本框限制只能输入数字和字母,退格键:

选择文本框的KeyPress事件,添加以下代码

 

[csharp] view plain copy
  1. private void textBox_KeyPress(object sender, KeyPressEventArgs e)  
  2.          
  3.             if ((e.KeyChar >= 'a' && e.KeyChar <= 'z'|| (e.KeyChar >= 'A' && e.KeyChar <= 'Z' 
  4.                 || (e.KeyChar >= '0' && e.KeyChar <= '9'|| (e.KeyChar == 8))  
  5.              
  6.                 e.Handled false 
  7.              
  8.             else  
  9.              
  10.                 e.Handled true 
  11.              
  12.         }  
================================================================
KeyPressEventArgs.Handled 属性 
获取或设置一个值,该值指示是否处理过 KeyPress 事件。
属性值
类型:System.Boolean
如果处理过事件,则为 true;否则为 false。
备注
如果未处理事件,则会将它发送到操作系统进行默认处理。将 Handled 设置为 true,以取消 KeyPress 事件。
以上来源MSDN。
解释:
就是说如果将Handled 设为True,那么KeyPress事件将会取消,这样就是说你按下了某个个按键了,但是系统不处理了,等于没按!!!

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有