简体   繁体   English

Maskedtextbox忽略e.Handled = true /如何防止输入

[英]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.

相关问题 如何在不使用e.Handled = true的情况下防止两次调用Closing事件? - How to prevent the twice-call of Closing event without using e.Handled=true? 如何检测何时其他代码设置e.Handled = true? - How To Detect When Other Code Setting e.Handled = true? 是否可以在RelayCommand中设置e.Handled = true? - Is it possible to set e.Handled = true in a RelayCommand? 为什么 e.Handled = true 不起作用? - Why e.Handled = true not working? 即使设置e.Handled = true,焦点也会改变 - Focus changes even after setting e.Handled = true 为什么在这种情况下 e.Handled = true 不会停止密钥处理? - Why does e.Handled = true not stop key processing in this case? 如何在Silverlight中访问按钮的e.handled? - How can I access to e.handled for a button in silverlight? 自定义WF4活动:为什么DoubleClick中的e.Handled = true不会停止冒泡 - Custom WF4 Activity: Why e.Handled = true in DoubleClick don't stops bubbling 将UserControl气泡中的MouseDoubleClick事件中的e.handled = true设置为父控件 - Setting e.handled = true in the MouseDoubleClick event in UserControl bubbles to Parent Control 异步以延迟在KeyDown事件中处理 - async to delay e.handled in a KeyDown event
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM