[英]Maskedtextbox ignores e.Handled = true /How to prevent input
I have a masked textbox (Note: NO normal Texbox, i searched the web and found only articles which are related to a normal textbox): 我有一个带遮罩的文本框(注意: 没有普通的Texbox,我在网上搜索并仅找到与普通文本框相关的文章):
this.maskedTextBox1 = new System.Windows.Forms.MaskedTextBox();
this.maskedTextBox1.Location = new System.Drawing.Point(12, 30);
this.maskedTextBox1.Mask = "??????????????????????????"; //Just as example
this.maskedTextBox1.Name = "maskedTextBox1";
this.maskedTextBox1.Size = new System.Drawing.Size(260, 20);
this.maskedTextBox1.TabIndex = 0;
this.maskedTextBox1.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.maskedTextBox1_KeyPress);
I want to prevent input to i casue i want to handle the input myself. 我想防止输入,我想自己处理输入。 The problem is, that this seems to be not possible.
问题是,这似乎是不可能的。 I tried to set e.Handled to true, but it totally ignores it.
我试图将e.Handled设置为true,但是它完全忽略了它。
private void maskedTextBox1_KeyPress(object sender, KeyPressEventArgs e)
{
e.Handled = true;
}
Is there a way to prevent input into a masked texbox? 有没有办法防止输入到屏蔽的texbox中? Also does anyone know why setting e.Handled = trues has no effect?
还有人知道为什么设置e.Handled = trues无效吗?
Many thanks in advance 提前谢谢了
You can try the KeyDown
event instead and set SuppressKeyPress = true;
您可以改用
KeyDown
事件并设置SuppressKeyPress = true;
: :
private void maskedTextBox1_KeyDown(object sender, KeyEventArgs e){
e.SuppressKeyPress = true;
}
I have suffered the same problem. 我遇到了同样的问题。
private void maskedTextBox1_KeyDown(object sender, KeyEventArgs e){
e.SuppressKeyPress = true;
}
In this case you get key scan codes which also includes the 'movement' keys. 在这种情况下,您将获得按键扫描代码,其中还包括“运动”按键。 I found out that if you use the normal
KeyPress
function and you set instead of: 我发现,如果您使用普通的
KeyPress
函数并设置为:
e.Handled = true
then 然后
e.KeyChar = (char) 0x00;
Now you get your requested function and I got mine 现在您获得了所需的功能,我得到了我的功能
Example: (Hex input with separation): 示例:(十六进制输入,带分隔符):
// mtxtBoxKey.Mask = @"CC CC CC CC CC CC CC CC CC CC CC CC CC CC CC CC CC CC CC CC CC CC CC CC";
private void mtxtBoxKey_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar >= 'a' && e.KeyChar <= 'f')
e.KeyChar = (char)(e.KeyChar - ' ');
if (!((e.KeyChar == 0x08) || (e.KeyChar >= '0' && e.KeyChar <= '9') || (e.KeyChar >= 'A' && e.KeyChar <= 'F')))
e.KeyChar = (char) 0x00;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.