开发者

msgbox when user hits enter in visual basic 2010

开发者 https://www.devze.com 2022-12-13 04:23 出处:网络
I just want to alert when user hit \"enter\" key. I tried this one i开发者_JS百科n keyup event, If e.KeyCode = Keys.Enter Then

I just want to alert when user hit "enter" key.

I tried this one i开发者_JS百科n keyup event,

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. In the designer, select the form and set the AcceptButton to your button. No code is required.

Note that the CancelButton has a similar usage, it is hard-wired to the Escape key.


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:

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. 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

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. Otherwise it is consumed by whatever control has focus.

0

精彩评论

暂无评论...
验证码 换一张
取 消