按键预览和接受按钮

发布于 2024-07-17 04:27:08 字数 365 浏览 6 评论 0原文

使用 winforms,我已将 KeyPreview 属性设置为 true,并为基本表单中的正确按键事件提供了事件句柄。

在从它继承的表单中,我根据应用程序的要求设置了 AcceptButton 属性。

在某些情况下,我希望 Enter 键具有与 AcceptButton 不同的功能。

我希望捕获基本表单中的回车键按下情况,并检查我不希望触发 AcceptButton 事件的特殊情况。

不过,似乎 AcceptButton 单击是在我的 basef 表单中的任何关键事件之前触发的。 我可以将功能写入可能的接受按钮的单击事件中,但是,在我看来,这将是一种黑客行为。

有什么建议么?

谢谢。

Using winforms, I have set the KeyPreview property to true and have event handles for the proper key events within the base form as well.

Within the forms that inherit from it, I set the AcceptButton property based on the requirements of the application.

There are certain cases in which I want the enter key to have functionality different than that of the AcceptButton.

I was hoping to capture the enter key press within my base form and check for the special cases where I do not want the AcceptButton event to fire.

It appears though, that the AcceptButton click is fired before any of the key events within my basef form. I could write functionality into the click events of the possible acceptbuttons, but, in my opinion, that would be a hack.

Any suggestions?

Thanks.

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

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

发布评论

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

评论(2

凉宸 2024-07-24 04:27:08

处理此问题的另一种方法是重写表单的 ProcessDialogKey() 方法,您可以在其中抑制接受和/或取消按钮。 例如,我有一个带有过滤器编辑器的应用程序,可以根据用户输入过滤网格。 我希望当过滤器编辑器控件获得焦点来应用过滤器时,用户能够按回车键。 问题是接受按钮代码运行并关闭表单。 下面的代码解决了这个问题。

    protected override bool ProcessDialogKey(Keys keyData)
    {
        // Suppress the accept button when the filter editor has the focus.
        // This doesn't work in the KeyDown or KeyPress events.
        if (((keyData & Keys.Return) == Keys.Return) && (filterEditor.ContainsFocus))
            return false;

        return base.ProcessDialogKey(keyData);
    }

您可以通过将以下代码放入基本对话框表单中来更进一步。 然后,您可以根据需要抑制子类中控件的接受按钮。

    private readonly List<Control> _disableAcceptButtonList = new List<Control>();

    protected override bool ProcessDialogKey(Keys keyData)
    {
        if (((keyData & Keys.Return) == Keys.Return) && (_disableAcceptButtonList.Count > 0))
        {
            foreach (Control control in _disableAcceptButtonList)
                if (control.ContainsFocus)
                    return false;
        }

        return base.ProcessDialogKey(keyData);
    }

    protected virtual void DisableAcceptButtonForControl(Control control)
    {
        if (!_disableAcceptButtonList.Contains(control))
            _disableAcceptButtonList.Add(control);
    }

Another way to handle this is to override the form's ProcessDialogKey() method where you can suppress the accept and/or cancel buttons. For example, I have an application with a filter editor that filters a grid based on user input. I want the user to be able to hit the return key when the filter editor control has the focus to apply the filter. The problem is the accept button code runs and closes the form. The code below resolves the issue.

    protected override bool ProcessDialogKey(Keys keyData)
    {
        // Suppress the accept button when the filter editor has the focus.
        // This doesn't work in the KeyDown or KeyPress events.
        if (((keyData & Keys.Return) == Keys.Return) && (filterEditor.ContainsFocus))
            return false;

        return base.ProcessDialogKey(keyData);
    }

You can take this even further by dropping the following code in a base dialog form. Then you can suppress the accept button for controls in subclasses as necessary.

    private readonly List<Control> _disableAcceptButtonList = new List<Control>();

    protected override bool ProcessDialogKey(Keys keyData)
    {
        if (((keyData & Keys.Return) == Keys.Return) && (_disableAcceptButtonList.Count > 0))
        {
            foreach (Control control in _disableAcceptButtonList)
                if (control.ContainsFocus)
                    return false;
        }

        return base.ProcessDialogKey(keyData);
    }

    protected virtual void DisableAcceptButtonForControl(Control control)
    {
        if (!_disableAcceptButtonList.Contains(control))
            _disableAcceptButtonList.Add(control);
    }
年华零落成诗 2024-07-24 04:27:08

作为我们的解决方法,我们捕获了我们想要覆盖接受按钮功能的控件的进入和离开事件。 在 Enter 事件中,我们将当前接受按钮保存在私有变量中,并将接受按钮设置为 null。 休假时,我们会将接受按钮重新分配回我们持有的私有变量。

KeyPreview 事件可以执行与上面类似的操作。 如果有人有更优雅的解决方案,我仍然很想知道。

谢谢。

As our workaround, we captured the enter and leave event for the control that we wanted to have override the acceptbutton functionality. Inside the enter event, we held the current accept button in a private variable and set the acceptbutton to null. On leave, we would reassign the acceptbutton back to the private variable we were holding.

The KeyPreview events could have done something similar to the above. If anyone has a more elegant solution, I would still love to know.

Thanks.

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