本文介绍了在c#中的文本框中仅允许Integer值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
您好,我希望只允许在文本框中输入整数值,在C#中没有小数点/点/点。直到现在我做到了这一点。有人可以帮助我。会是一个很大的帮助。现在我只需要阻止小数部分。
Hello, I want allow only to enter Integer value in a textbox with no decimals/points/dot in C#. Till now I did this far. Can someone assist me on this. would be a great help. Now I just need to prevent the decimal part.
private void Quantity_txt_KeyPress(object sender, KeyPressEventArgs e)
{
char ch = e.KeyChar;
if (!Char.IsDigit(ch) && ch != 8 && ch != 46)
{
e.Handled = true;
}
}
推荐答案
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
e.Handled = ((new Char[] {'0','1','2','3','4','5','6','7','8','9'}).ToList<Char>()).Contains(e.KeyChar)?false:true;
//you can either change it to
e.Handled = char.IsDigit(e.KeyChar) ? false : true;
}
这篇关于在c#中的文本框中仅允许Integer值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!