繁体   English   中英

如何检测按钮上的ENTER键

[英]How to detect ENTER key on button

我正在使用此代码。 panelButtons []是按钮的数组。

        for (i = 0; i <= 15; i++)
        {
            int temp = i;
            panelButtons[temp].KeyPress += new KeyPressEventHandler(checkKeyPress);
        }
    }
    private void checkKeyPress(object a, KeyPressEventArgs b)
    {
        if(b.KeyChar==(char)Keys.Enter || b.KeyChar==(char)Keys.Return)
        {
            DialogResult result = MessageBox.Show("Enter");
        }
    }

问题是,它没有检测到回车键。 我在网上进行了大量搜索,但是我所能获得的只是如何检测文本框上的ENTER键。 此方法以及与此类似的许多其他方法(如KeyDown想法)均不起作用。

处理该按钮的PreviewKeyDown-Event,并将PreviewKeyDownEventArgs.IsInputKey设置为True

Private Sub Button1_PreviewKeyDown(sender As Object, e As PreviewKeyDownEventArgs) Handles Button1.PreviewKeyDown
    If e.KeyCode = Keys.Enter Then e.IsInputKey = True
End Sub

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM