C#:Form.AcceptButton 出现问题

发布于 2024-08-18 22:55:20 字数 173 浏览 4 评论 0原文

我有一个带有按钮的表单,该按钮设置为表单的 AcceptButton。该表单还有其他几个控件。现在,当我在其他控件上按 Enter 时,由于表单上的接受按钮,表单会关闭。取消按钮也是如此。我该如何处理这个问题。我尝试连接表单和控件的按键 keydown 事件。没有一个有效。有解决这个问题的方法吗?

非常感谢, 达特

I have a form with an button which is set as the AcceptButton of the form. The form has several other controls. Now when I press Enter on other controls the form gets closed because of the accept button on the form. Same goes for CancelButton. How do I handle this. I tried hooking on to keypress keydown event of the form and controls. None works. Any work around for this?

Thanks a ton,
Datte

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

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

发布评论

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

评论(5

煞人兵器 2024-08-25 22:55:20

这就是 AcceptButton 属性的工作原理。它指定每当您按 时自动单击的按钮。

如果您不希望出现此行为,请不要将其设置为 AcceptButton。没有其他理由这样做。

That is how the AcceptButton property works. It specifies the button that is automatically clicked whenever you press <Enter>.

If you don't want this behaviour, don't set it as the AcceptButton. There is no other reason to do it.

晌融 2024-08-25 22:55:20

不太确定您期望表单如何运行,但是您可以执行类似以下操作来对事物有更多的控制:

    protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
    {
        if (keyData == Keys.Enter)
        {
            // do something
        }
        if (keyData == Keys.Escape)
        {
            // do something else
        }
        return base.ProcessCmdKey(ref msg, keyData);
    }

Not exactly sure about how you expect your form to function, but you could do something like the following to have a little more control over things:

    protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
    {
        if (keyData == Keys.Enter)
        {
            // do something
        }
        if (keyData == Keys.Escape)
        {
            // do something else
        }
        return base.ProcessCmdKey(ref msg, keyData);
    }
明月夜 2024-08-25 22:55:20

您可以从表单中删除 AcceptButton,并在表单上设置处理其 KeyDown 事件的 KeyPreview 属性。您可以在此处检查 Enter 键并采取相应的操作。

You can remove AcceptButton from form and set the KeyPreview property on the form that'll handle its KeyDown event. There you can check for the Enter key and take the action accordingly.

GRAY°灰色天空 2024-08-25 22:55:20

这是表单的功能之一,即

如果按钮没有焦点,如果您仍然希望在用户单击 Enter 时执行所需的代码...

设置表单的 AcceptButton 属性以允许用户通过按即使按钮没有焦点,也请输入 ENTER。

问候。

This is one of the feature of the form i.e.

if button does not have a focus if you still want desired code to be executed when user click Enter...

Set the AcceptButton property of a form to allow users to click a button by pressing the ENTER even if the button does not have focus.

Regards.

风透绣罗衣 2024-08-25 22:55:20

在 VB>net 中试试这个

  If CType(Me.ActiveControl, Button).Name = Button1.Name Then

        End If

Try This One In VB>net

  If CType(Me.ActiveControl, Button).Name = Button1.Name Then

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