制作十六进制掩码后,不允许使用 CTRL+C 和 CTRL+V

发布于 2024-09-18 16:59:52 字数 1112 浏览 13 评论 0原文

我有一个小问题。制作十六进制掩码后,我无法使用 Ctrl+C/V 复制/粘贴。如果我右键单击文本框,我可以粘贴。但我希望能够只按 Ctrl+V

如果我删除十六进制掩码, Ctrl+C/V 工作正常。

这是一些代码:

private void maskedTextBox1(Object sender, System.Windows.Forms.KeyPressEventArgs e)
    {
        // this will allow a-f, A-F, 0-9, "," 
        if (!System.Text.RegularExpressions.Regex.IsMatch(e.KeyChar.ToString(), "^[0-9a-fA-F,V,C,'\b']+$"))
            {
                e.Handled = true;
            }

        // if keychar == 13this ill allow <ENTER>
        if (e.KeyChar == (char)13)
            {
            button1_Click(sender, e);
            }
        // I thought I could fix it with the lines below but it doesnt work
       /* if (e.KeyChar == (char)22)
        {
            // <CTRL + C> 
            e.Handled = true;
        }
        if (e.KeyChar == (char)03)
        {
            // is <CTRL + V>
            e.Handled = true;
        }*/
        //MessageBox.Show(((int)e.KeyChar).ToString());
    }

有人可以给我一些提示吗?

I have a small problem. After making a hex mask, I can not copy/paste with Ctrl+C/V. If I right click in the textbox I can paste. But I would like to be able to just press Ctrl+V.

If I delete the hex mask, Ctrl+C/V works fine.

Here is a bit of the code:

private void maskedTextBox1(Object sender, System.Windows.Forms.KeyPressEventArgs e)
    {
        // this will allow a-f, A-F, 0-9, "," 
        if (!System.Text.RegularExpressions.Regex.IsMatch(e.KeyChar.ToString(), "^[0-9a-fA-F,V,C,'\b']+$"))
            {
                e.Handled = true;
            }

        // if keychar == 13this ill allow <ENTER>
        if (e.KeyChar == (char)13)
            {
            button1_Click(sender, e);
            }
        // I thought I could fix it with the lines below but it doesnt work
       /* if (e.KeyChar == (char)22)
        {
            // <CTRL + C> 
            e.Handled = true;
        }
        if (e.KeyChar == (char)03)
        {
            // is <CTRL + V>
            e.Handled = true;
        }*/
        //MessageBox.Show(((int)e.KeyChar).ToString());
    }

Could someone give me some hints, Please?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

旧时光的容颜 2024-09-25 16:59:52

您需要使用 KeyDown 事件处理程序而不是 KeyPressed 来捕获这些击键。 KeyPressed 仅在键入按键时才会引发。

MaskedTextBox 在这里并不理想,您也可以使用常规 TextBox 来实现。使用验证事件来格式化数字并检查范围。例如:

    private void textBox1_KeyPress(object sender, KeyPressEventArgs e) {
        bool ok = e.KeyChar == 8;  // Backspace
        if ("0123456789ABCDEF".Contains(char.ToUpper(e.KeyChar))) ok = true;
        if (!ok) e.Handled = true;
    }

    private void textBox1_Validating(object sender, CancelEventArgs e) {
        int value;
        if (textBox1.Text.Length > 0) {
            if (!int.TryParse(this.textBox1.Text, System.Globalization.NumberStyles.HexNumber, null, out value)) {
                this.textBox1.SelectAll();
                e.Cancel = true;
            }
            else {
                textBox1.Text = value.ToString("X8");
            }
        }
    }

You need to catch these keystrokes with a KeyDown event handler, not KeyPressed. KeyPressed is only raised for typing keys.

A MaskedTextBox is not ideal here, you can also do it with a regular TextBox. Use the Validating event to format the number and check for range. For example:

    private void textBox1_KeyPress(object sender, KeyPressEventArgs e) {
        bool ok = e.KeyChar == 8;  // Backspace
        if ("0123456789ABCDEF".Contains(char.ToUpper(e.KeyChar))) ok = true;
        if (!ok) e.Handled = true;
    }

    private void textBox1_Validating(object sender, CancelEventArgs e) {
        int value;
        if (textBox1.Text.Length > 0) {
            if (!int.TryParse(this.textBox1.Text, System.Globalization.NumberStyles.HexNumber, null, out value)) {
                this.textBox1.SelectAll();
                e.Cancel = true;
            }
            else {
                textBox1.Text = value.ToString("X8");
            }
        }
    }
眼泪淡了忧伤 2024-09-25 16:59:52

您有:

if (e.KeyChar == (char)03)
    {
        // is <CTRL + V>
        e.Handled = true;
    }*/

根据 思科
Ctrl+V 的值为 22 所以你应该有:

if (!System.Text.RegularExpressions.Regex.IsMatch(e.KeyChar.ToString(), "^[0-9a-fA-F,V,C,'\b']+$") && e.KeyChar != 22)
        {
            e.Handled = true;
        }

You had:

if (e.KeyChar == (char)03)
    {
        // is <CTRL + V>
        e.Handled = true;
    }*/

According to Cisco
the value for Ctrl+V is 22 so you should have:

if (!System.Text.RegularExpressions.Regex.IsMatch(e.KeyChar.ToString(), "^[0-9a-fA-F,V,C,'\b']+$") && e.KeyChar != 22)
        {
            e.Handled = true;
        }
情未る 2024-09-25 16:59:52

MaskedTextBox 可能会阻止 Ctrl+V 调用(否则您可以轻松绕过掩码)。就我个人而言,我不会使用屏蔽文本框,而是单独验证输入,并在输入出现问题时提醒用户。 MaskedTextBox 在一般使用中存在缺点,因为它不是用户习惯的普通组件,用户更习惯被告知输入错误。

The MaskedTextBox might block the Ctrl+V call (otherwise you could easily circumvent the mask). Personally, I wouldn't use a masked textbox but validate the input seperately, and alert the user if there is a problem with the input. The MaskedTextBox has drawbacks in general use, as it isn't a normal component the user is used to, a user is more used to being told that an input was wrong.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文