当我按任意键时中断低级字符 [英] interrupt lower char when I press any key
本文介绍了当我按任意键时中断低级字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用WPF处理文本框,我只想按A,B,C,D,E,F和0-9表示十六进制数。但在我的keydown事件中,我不能禁用低级字符a,b,c,d,e,f。我的代码在下面:
I am working on a textbox using WPF where I want to press only A,B,C,D,E,F and 0-9 that means hexadecimal number. But in my keydown event I can''t disable lower char a,b,c,d,e,f. My code is belown:
private void txtHex_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key >= Key.A && e.Key <= Key.F)
{
e.Handled = false;
}
else if (e.Key < Key.D0 || e.Key > Key.D9)
{
if (e.Key < Key.NumPad0 || e.Key > Key.NumPad9)
{
e.Handled = true;
}
}
}
如何解决这个问题?
提前致谢。
How can I solve this?
Thanks in advance.
推荐答案
将TextBox的CharacterCasing属性更改为Upper
Change the CharacterCasing property of the TextBox to "Upper"
尝试使用PreviewTextInput [ ^ ]。
这个事件让你分辨小和大写字母,因为它显示输入文本,你仍然可以通过Handled
属性取消输入。
Instead of using KeyDown, try using PreviewTextInput[^].
This event let''s you to distinguish small and capital letters because it reveals the input text and you can still cancel the input viaHandled
property.
这篇关于当我按任意键时中断低级字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文