简体   繁体   English

验证KeyPress事件处理程序中的字符

[英]Validating characters in KeyPress event handler

I have a form with MaskedTextBoxField where I would like to enable adding only hexadecimal values. 我有一个MaskedTextBoxField表单,我想在其中启用仅添加十六进制值的功能。 Length of the hexadecimal value is fixed to 16 characters, first six digits are always the same, and there is a dash after every two characters. 十六进制值的长度固定为16个字符,前六位数字始终相同,每两个字符后有一个破折号。

I have set Mask property on this field to "\\0\\0-15-4F-AA-AA-AA-AA-AA" to allow only alphanumerics. 我在此字段上将Mask属性设置为“ \\ 0 \\ 0-15-4F-AA-AA-AA-AA-AA”,仅允许使用字母数字。 But this way, I am able to enter all letters, not only "af" and "AF". 但是通过这种方式,我可以输入所有字母,不仅是“ af”和“ AF”。

In KeyPress event is following code: 在KeyPress事件中是以下代码:

char c = e.KeyChar;
if (('G' <= c && c <= 'Z') || ('g' <= c && c <= 'z'))
{
  e.Handled = true;
}

When I run this code, application gets to this part, but even then, characters 'GZ' and 'gz' are still entered in MaskedTextBox. 当我运行这段代码时,应用程序进入了这一部分,但是即使这样,字符“ GZ”和“ gz”仍会输入到MaskedTextBox中。 How can I prevent this characters being accepted in this MaskedTextBox? 如何防止此MaskedTextBox中的字符被接受?

use keycode insted of keychar so you can essaly handle this code 使用的键码 insted的keychar的,所以你可以essaly处理这个代码
e.keycode; e.keycode;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM