本文介绍了在文本框中允许使用逗号和点字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我希望文本框在KeyPress事件上允许字母,退格,空格,逗号和点...
请给我建议...
我的代码是这样的
I want textbox to allow letters,backspace,space,commas and dots on KeyPress Event ...
Please give me suggestion for this...
I have my code something like this
public void Check(Object sender, KeyPressEventArgs e)
{
if (!char.IsLetter(e.KeyChar))
{
e.Handled = true;
}
if (char.IsLetter(e.KeyChar) || e.KeyChar == (char)Keys.Back || e.KeyChar == (char)Keys.Space )
{
&
// These characters may pass
e.Handled = false;
}
else
{
// Everything that is not a letter, nor a backspace nor a space will be blocked
e.Handled = true;
}
}
推荐答案
public void Check(Object sender, KeyPressEventArgs e)
{
//if (!char.IsLetter(e.KeyChar)||!char.IsNumber(e.KeyChar))
//{
// e.Handled = true;
//}
if (char.IsLetter(e.KeyChar) || e.KeyChar == (char)Keys.Back || e.KeyChar == (char)Keys.Space || e.KeyChar=='.' || e.KeyChar==',' ||char.IsNumber(e.KeyChar))
{
// These characters may pass
e.Handled = false;
}
else
{
// Everything that is not a letter, nor a backspace nor a space will be blocked
e.Handled = true;
}
}
这篇关于在文本框中允许使用逗号和点字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!