自定义数字文本框 [英] Custom Numeric TextBox
本文介绍了自定义数字文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经在C#.net中创建了一个数字文本框用户控件,它可以按我的需要工作,但是我无法使用Delete和Backspace按钮从该文本框中删除值.
I Have created a numeric textbox usercontrol in C#.net its working as i need but i m not able to delete values from that textbox using delete and backspace button
推荐答案
我不知道您的代码是什么样子,即使我在退格时被卡住了一次.这是仅允许数字值的代码示例-
I have no idea what your code looks like, anyway even I was stuck once while doing backspace. Here is a sample example of a code to allow only numeric values-
protected override void OnKeyDown(KeyEventArgs e)
{
//To only allow numbers
if (char.IsNumber((char)e.KeyValue) || (((char)e.KeyData) == '\b'))
{
e.SuppressKeyPress = false;
}
else { e.SuppressKeyPress = true; }
}
看看If条件的第二部分-((char)e.KeyData) == ''\b'')
这将允许您在文本框中使用退格键.
希望这会有所帮助.
我已经创建了一个在我的博客中解释过的控件- ^ ]
Have a look at the second part of the If condition - ((char)e.KeyData) == ''\b'')
This will allow you to use backspace in your textbox.
Hope this helps.
I have created one such control explained in my blog here - http://tarundotnet.wordpress.com/2011/06/26/allow-numbers-or-letters-and-disable-right-click-in-textbox/[^]
这篇关于自定义数字文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文