C#:在表单上使用 TextPreview 时,TextBox 未接收键

发布于 2024-12-04 11:04:55 字数 1532 浏览 1 评论 0原文

我正在用 C# 在 Windows 窗体中实现搜索功能。我已在表单上将 KeyPreview 设置为 true,并为 KeyDown 添加了一个事件处理程序,这样我就可以捕获 ctrl+f等内容>esc输入

我很好地抓住了这些键,并且能够显示我的文本框,但我无法在框中输入内容。所有键都将发送至 PortsTraceForm_KeyDown(...),但它们从未到达文本框。根据有关 KeyPreview 的 msdn 页面,将 e.Handled 设置为 false 应该会导致事件传递到焦点视图(文本框),但这并没有发生。我尚未为文本框注册 KeyDown 事件,因此它应该使用默认行为。我错过了什么吗?

KeyDown 事件:

    private void PortsTraceForm_KeyDown(object sender, KeyEventArgs e)
    {
        e.SuppressKeyPress = true;
        e.Handled = false;

        if (e.KeyData == (Keys.F | Keys.Control)) // ctrl+f
        {
            e.Handled = true;
            ShowSearchBar();
        }
        else if (e.KeyCode == Keys.Escape) // esc
        {
            e.Handled = true;
            HideSearchBar();
        }
        else if (e.KeyCode == Keys.Enter) // enter
        {
            if (searchPanel.Visible)
            {
                e.Handled = true;
                if (searchShouldClear)
                    SearchStart();
                else
                    SearchNext();
            }
        }
    }

显示搜索栏:

    private void ShowSearchBar()
    {
            FindBox.Visible = true;
            FindBox.Focus(); // focus on text box   
    }

隐藏搜索栏:

    private void HideSearchBar()
    {
            this.Focus(); // focus on form
            FindBox.Visible = false;
    }

I am implementing a search function in a windows form in c#. I have set KeyPreviewto true on the form and have added an event handler for KeyDown so I can catch things like ctrl+f, esc and enter.

I am catching these keys just fine and I'm able to make my text box appear, but I am unable to type into the box. All of the keys are going to PortsTraceForm_KeyDown(...) but they never make it to the text box. According to the msdn page about KeyPreview, setting e.Handled to false should cause the event to pass to the view in focus (the text box), but this isn't happening. I have not registered a KeyDown event for the text box, so it should be using the default behavior. Have I missed something?

KeyDown event:

    private void PortsTraceForm_KeyDown(object sender, KeyEventArgs e)
    {
        e.SuppressKeyPress = true;
        e.Handled = false;

        if (e.KeyData == (Keys.F | Keys.Control)) // ctrl+f
        {
            e.Handled = true;
            ShowSearchBar();
        }
        else if (e.KeyCode == Keys.Escape) // esc
        {
            e.Handled = true;
            HideSearchBar();
        }
        else if (e.KeyCode == Keys.Enter) // enter
        {
            if (searchPanel.Visible)
            {
                e.Handled = true;
                if (searchShouldClear)
                    SearchStart();
                else
                    SearchNext();
            }
        }
    }

show search bar:

    private void ShowSearchBar()
    {
            FindBox.Visible = true;
            FindBox.Focus(); // focus on text box   
    }

hide search bar:

    private void HideSearchBar()
    {
            this.Focus(); // focus on form
            FindBox.Visible = false;
    }

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

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

发布评论

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

评论(2

┊风居住的梦幻卍 2024-12-11 11:04:55

即使您正在调用 Focus(),您的 TextBox 也可能没有焦点。来自文档:

Focus 是一种低级方法,主要供自定义控件作者使用。相反,应用程序程序员应该使用子控件的 Select 方法或 ActiveControl 属性,或者窗体的 Activate 方法。

您可以检查 Focus() 的返回值是否成功,但我过去使用该方法将焦点设置到任意控件时运气不佳。相反,请尝试使用文档建议的方法,即调用 Select()

编辑:

没关系(尽管它仍然是有效的建议),我想我看到你的问题:

e.SuppressKeyPress = true

你为什么这样做?再次,来自文档:

[SuppressKeyPress] 获取或设置一个值,指示是否应将按键事件传递给底层控件

。因此,您有意阻止 TextBox 获取按键事件。如果您想传递事件,则不应将该属性设置为 false

Your TextBox likely does not have focus even though you are calling Focus(). From the documentation:

Focus is a low-level method intended primarily for custom control authors. Instead, application programmers should use the Select method or the ActiveControl property for child controls, or the Activate method for forms.

You can check the return value of Focus() for success, but I have had little luck in the past using that method to set focus to an arbitrary control. Instead, try using the method that the documentation suggests, i.e., call Select().

EDIT:

Nevermind (though it's still valid advice), I think I see your problem:

e.SuppressKeyPress = true

Why are you doing this? Again, from the docs:

[SuppressKeyPress] Gets or sets a value indicating whether the key event should be passed on to the underlying control

So you are intentionally preventing the TextBox from getting key events. If you want to pass the event through you shouldn't be setting that property to false.

孤檠 2024-12-11 11:04:55

尝试这个例子,覆盖方法。

    protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
    {
        // your code here

        // this is message example
        MessageBox.Show(keyData.ToString());
        return base.ProcessCmdKey(ref msg, keyData);
    }

问候。

try this example , of overrides method.

    protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
    {
        // your code here

        // this is message example
        MessageBox.Show(keyData.ToString());
        return base.ProcessCmdKey(ref msg, keyData);
    }

Regards.

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