如何使多行文本框不接受 Enter 和 Backspace

发布于 2024-11-01 10:57:00 字数 1073 浏览 3 评论 0原文

我有一个多行文本框,不应接受字母、数字、换行符(输入键)和退格键。简而言之,textbox.Text 不应该是可编辑的。但我确实希望文本框接受两个快捷键 - control 和 control+R。使文本框不可编辑的一种方法是将其设为只读。但是文本框根本不会接受任何击键。简而言之,我的快捷键(control和control+R)在只读方法中不起作用(Control+R)。

任何人都可以在这方面提供帮助。这就是我所要做的。

我在这里可以做的一件事是不要将文本框设置为只读,然后限制可以在文本框中输入的字符(字母和数字)。使用此代码:

    private void txtResult_KeyPress(object sender, KeyPressEventArgs e)
    {
        // only modifier keys, escape, enter, backspace etc is accepted now
        e.Handled = !char.IsControl(e.KeyChar);
    }

    private void txtResult_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.Control == true)
        {
            if (e.KeyCode == Keys.R)
            {
                // do something
            }
            else
            {
                //do something
            }
        }

        if (e.KeyCode == Keys.Escape)
        {
            //do something
        }
    }

通过此技术,我可以使快捷键(control 和 control+R)正常工作。但这种方法的问题是 Enter 和 Backspace 键也可以编辑文本框的文本。如何专门限制从文本框中注册 Enter 和 Backspace 键,但让 Control 和 Escape 键注册?

I have a multiline textbox which shouldn't accept alphabets, numbers, newline(enter key) and backspace. In short, the textbox.Text shouldn't be editable. But I do want the textbox to accept two shortcut keys - control and control+R. One way I can make the textbox un-editable is by making it read-only. But then the textbox wont accept any keystroke at all. In short, my shortcuts ( control and control+R) wont work( Control + R) with read-only method.

Can anyone help in this regard. That's all I have to do.

One thing I could do here is not to make the textbox read-only and then restrict the characters(alphabets and digits) that could be inputted in the textbox. With this code:

    private void txtResult_KeyPress(object sender, KeyPressEventArgs e)
    {
        // only modifier keys, escape, enter, backspace etc is accepted now
        e.Handled = !char.IsControl(e.KeyChar);
    }

    private void txtResult_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.Control == true)
        {
            if (e.KeyCode == Keys.R)
            {
                // do something
            }
            else
            {
                //do something
            }
        }

        if (e.KeyCode == Keys.Escape)
        {
            //do something
        }
    }

With this technique I can get the shortcuts(control and control+R) working. But the trouble with this method is that Enter and Backspace keys work as well making it possible to edit the text of textbox. How can I specifically restrict Enter and Backspace key being registered from the textbox, but let Control and Escape??

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

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

发布评论

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

评论(3

烂人 2024-11-08 10:57:00

您尝试过 SuppressKeyPress = true 吗?

private void textBox2_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Control == true)
            {
                if (e.KeyCode == Keys.R)
                {
                    // do something
                }
                else
                {
                    //do something
                }
            }
            else
                e.SuppressKeyPress = true;


        }

did you try SuppressKeyPress = true ?

private void textBox2_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Control == true)
            {
                if (e.KeyCode == Keys.R)
                {
                    // do something
                }
                else
                {
                    //do something
                }
            }
            else
                e.SuppressKeyPress = true;


        }
变身佩奇 2024-11-08 10:57:00

既然您正在 KeyDown 事件处理程序中处理按键,为什么不让您的 KeyPress 处理程序返回所有击键都已处理的信息呢?

所以无论如何只要设置 e.Handled = true 即可。我相信退格键和回车键也会被解释为控制字符。

Since you are handling the keys in the KeyDown event handler, why not have your KeyPress handler return that all keystrokes are handled?

So just set e.Handled = true no matter what. I believe the backspace and enter would be interpreted as control characters, also.

记忆消瘦 2024-11-08 10:57:00

如果文本框设置为只读,则 EnterBackspace 键将不起作用,正如您在之前所做的问题中所建议的那样。确保该属性仍设置为 true。您可以在“属性”窗口中设置它,也可以通过如下代码进行设置:

myTextBox.ReadOnly = true;

然后,您需要处理文本框控件的 KeyDown 事件,并监视您感兴趣的特定键。像这样的事情:

private void myTextBox_KeyDown(object sender, KeyEventArgs e)
{
   if (e.Control)
   {
      if (e.KeyCode == Keys.R)
      {
         MessageBox.Show("Pressed Ctrl+R");
      }
      else
      {
         MessageBox.Show("Pressed Ctrl");
      }
   }
   else if (e.KeyCode == Keys.Escape)
   {
      MessageBox.Show("Pressed Esc");
   }
}

只要文本框设置为只读,这就会完全按照预期工作。无法识别其他键,并且用户无法更改或修改文本框中的任何文本。您不需要抑制按键,因为当您将其设置为只读时,控件已经这样做了。您也不需要同时处理 KeyDownKeyPress 事件。 KeyPress 无论如何都不适合你,因为它不允许你处理控制字符。

The Enter and Backspace keys won't work if the textbox is set to ReadOnly, as you suggested early on in the question that you had done. Make sure the property is still set to true. You can either set it in the Properties window, or through code like so:

myTextBox.ReadOnly = true;

Then, you need to handle the KeyDown event for the textbox control, and watch for the specific keys that you're interested in. Something like this:

private void myTextBox_KeyDown(object sender, KeyEventArgs e)
{
   if (e.Control)
   {
      if (e.KeyCode == Keys.R)
      {
         MessageBox.Show("Pressed Ctrl+R");
      }
      else
      {
         MessageBox.Show("Pressed Ctrl");
      }
   }
   else if (e.KeyCode == Keys.Escape)
   {
      MessageBox.Show("Pressed Esc");
   }
}

This works exactly as expected, as long as the textbox is set to read-only. No other keys are recognized, and the user cannot change or modify any of the text in the textbox. You don't need to suppress the keypresses, as the control is already doing that when you set it to read-only. You also don't need to handle both the KeyDown and KeyPress events. KeyPress won't work for you anyway, as it doesn't let you handle control characters.

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