简体   繁体   English

当用户点击进入visual basic 2010时的msgbox

[英]msgbox when user hits enter in visual basic 2010

I just want to alert when user hit "enter" key. 我只是想在用户点击“输入”键时发出警报。

I tried this one in keyup event, 我在keyup事件中试过这个,

If e.KeyCode = Keys.Enter Then
    MsgBox("msg")
End If

It didnt work, is that wrong ? 它没有用,是错的吗?

The Enter key has strictly defined use in UI design, it executes the "accept" action of a dialog. Enter键在UI设计中严格定义使用,它执行对话框的“接受”操作。 In the designer, select the form and set the AcceptButton to your button. 在设计器中,选择表单并将AcceptButton设置为您的按钮。 No code is required. 无需代码。

Note that the CancelButton has a similar usage, it is hard-wired to the Escape key. 请注意,CancelButton具有类似的用法,它与Escape键硬连线。

Not a complete answer but it may help. 不完整的答案,但它可能会有所帮助。 You may block Submission of the main form by exiting sub in case that another textBox is active: 如果另一个textBox处于活动状态,您可以通过退出sub来阻止提交主表单:

Private Sub btnSubmit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSubmit.Click

        ' do not submit anything in case that something else is active
        If txtItemPrompt.Focused() Then
            Exit Sub
        End If

        ' rest of the code
End Sub

It really depends what context you are applying to. 这实际上取决于您申请的背景。 The KeyUp event will only fire on a particular control, and bubble up to it's parent controls. KeyUp事件仅触发特定控件,并冒泡到它的父控件。 However, if focus is not set on the control you are handling the event on then the event will not fire. 但是,如果未在您正在处理事件的控件上设置焦点,则事件将不会触发。

Just need to makes sure you put the code inside a sub that Handles Me.KeyDown 只需要确保将代码放在Handles Me.KeyDown的子代码中

Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
        If e.KeyCode = Keys.Enter Then
            MsgBox("Enter Pressed")
        End If
    End Sub

You need to set the keypreview property of your form to true if you want to see the enter key at the form level. 如果要在表单级别查看回车键,则需要将表单的keypreview属性设置为true。 Otherwise it is consumed by whatever control has focus. 否则它被任何具有焦点的控件所消耗。

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

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