在文本框中允许使用逗号和点字符 [英] Allowing comma and Dot characters in Textbox
本文介绍了在文本框中允许使用逗号和点字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我希望文本框在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;
}
}
推荐答案
最好为该文本框使用正则表达式. 通过此链接可以更好地理解正则表达式..
http://msdn.microsoft.com/en-us/library/ff650303.aspx [ ^ ]
希望您能理解..
Its better to use the regular expression for that text box ..
Go through this link it will better useful to you to understand regular expression..
http://msdn.microsoft.com/en-us/library/ff650303.aspx[^]
I hope you have got it..
^ ]
使用此链接
http://www.regexlib.com/Search.aspx?k=special+characters&c=0&m=0&ps=20&p=2[^]
ues this link
您好,
Hi,
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;
}
}
这篇关于在文本框中允许使用逗号和点字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文