您的位置:首页 > 编程语言 > C#

C#中textbox只能输入数字

2011-06-21 10:01 267 查看
textbox只能输入数字

 

方法一:

private void textBox7_KeyPress(object sender, KeyPressEventArgs e)//只能输入数字和.

{

    byte[] array = System.Text.Encoding.Default.GetBytes(e.KeyChar.ToString());

    if (!char.IsDigit(e.KeyChar) || array.LongLength == 2) e.Handled = true;

    if (e.KeyChar == '/b' || e.KeyChar == '.') e.Handled = false;

}

 

方法二:

private void textBox7_KeyPress(object sender, KeyPressEventArgs e)//只能输入数字和.  但不能输入小数

{

    if (!(e.KeyChar >= '0' && e.KeyChar <= '9')) e.Handled = true;

    if (e.KeyChar == '/b' || e.KeyChar == '.') e.Handled = false;

}

 

方法三:

private void textBox7_KeyPress(object sender, KeyPressEventArgs e)//需求数量栏位,只能输入数字,并且可以输入小数

{

    if (e.KeyChar != 13 && e.KeyChar != 8 && !char.IsDigit(e.KeyChar) && e.KeyChar != 46)   e.Handled = true;

    int a = textBox7.Text.IndexOf('.');

    if (e.KeyChar == 46 && a > 0)  e.Handled = true;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  textbox c# object byte