的WinForms文本框自动完成事件文本框、自动完成、事件、WinForms

2023-09-06 05:21:25 作者:稳稳的幸福

我有一个.NET文本框的形式自动完成功能。该表格​​还具有 AcceptButton CancelButton 定义。如果我试图提交的建议与输入键或关闭的下拉 Esc键,我的窗体关闭。

I have a .NET TextBox with AutoComplete feature on the form. The form has also AcceptButton and CancelButton defined. If I try to commit a suggestion with Enter key or close drop down with Esc, my form closes.

我的想法是创建我的自定义文本从 TexBox 继承和捕捉输入键和逃生键,但只有当自动完成用户界面是可见的。我怎么会知道什么时候自动完成列表是可见的?

My idea is to create my custom textbox inheriting from TexBox and capture Enter key and the Escape key, but only when the autocomplete UI is visible. How could I know when the autocomplete list is visible?

public class MyTextBox : TextBox
{
    protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
    {
        if (IsOnAutoComplete())
        {
            if (keyData == Keys.Enter || keyData == Keys.Return)
            {
                return true;
            }
        }
        return base.ProcessCmdKey(ref msg, keyData);
    }
}

换句话说,你知道我怎么可能实施 IsOnAutoComplete()的方法?有任何文本框的活动注意到这一点?

In other words, do you know how could I implement IsOnAutoComplete() method? Has the textbox any events to notice this?

任何其他的解决办法是AP preciated。先谢谢了。

Any other solution would be appreciated. Thanks in advance.

推荐答案

一个简单的解决方案,我已经使用在过去是消除了接受和取消按钮协会在文本框中有焦点。

A simple solution I have used in the past is removing the Accept and Cancel button associations when the text box has focus.

    textBox1.Enter += (o, args) =>
                                    {
                                        AcceptButton = null;
                                        CancelButton = null;
                                    };
    textBox1.Leave += (o, args) =>
                                    {
                                        AcceptButton = btnOK;
                                        CancelButton = btnCancel;
                                    };