954,517 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Enable enter (return) on button

Hi Guys,

Probably a simple one but im struggling to find it in VS 2008. How do you enable a VB program to allow a user to press enter rather than click on the button?

I have a text box which the user types a number and then clicks a button which runs the script. I want the user to be able to either press enter once they enter a number or click on the button.

I thought there was a setting in properties to do this but I am struggling to find it - also no joy searching online yet.

Sorry if this is a very simple oversight on my part.

Dazamondo
Newbie Poster
5 posts since Jun 2010
Reputation Points: 10
Solved Threads: 0
 

Sorry just to clarify so the user is able to do both - press enter key and click the button.

Thanks

Dazamondo
Newbie Poster
5 posts since Jun 2010
Reputation Points: 10
Solved Threads: 0
 

Sorry just to clarify so the user is able to do both - press enter key and click the button.

Thanks

No at the moment they can only click on the button (enter doesnt do anything). I would like them to be able to press enter on the keyboard and also be able to click on the button.

Dazamondo
Newbie Poster
5 posts since Jun 2010
Reputation Points: 10
Solved Threads: 0
 

On the properties explorer You see acceptButton, then select the button

and if you want to use codes to do this you can use this function

Protected Overrides Function ProcessDialogKey(ByVal keyData As System.Windows.Forms.Keys) As Boolean

        Select Case (keyData)
            Case Keys.Enter
                'the action you want
                Return True
            
        End Select
        Return MyBase.ProcessDialogKey(keyData)
    End Function
meffe
Light Poster
41 posts since May 2010
Reputation Points: 10
Solved Threads: 8
 

On the properties explorer You see acceptButton, then select the button

and if you want to use codes to do this you can use this function

Protected Overrides Function ProcessDialogKey(ByVal keyData As System.Windows.Forms.Keys) As Boolean

        Select Case (keyData)
            Case Keys.Enter
                'the action you want
                Return True
            
        End Select
        Return MyBase.ProcessDialogKey(keyData)
    End Function
meffe
Light Poster
41 posts since May 2010
Reputation Points: 10
Solved Threads: 8
 

Thanks for these effective code
i tried it and it works but i could not get the control event fire exp.
i write a code inside keydown event for a button to close the form or save the data but i did not get the event fire
can you help me in this issue?
thanks

mgomaa
Newbie Poster
1 post since Aug 2010
Reputation Points: 10
Solved Threads: 0
 

See if this helps.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        MsgBox("A cool little message.")
    End Sub

    Private Sub TextBox1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyUp
        If e.KeyCode = Keys.Enter Then
            Button1_Click(sender, e)
        End If
    End Sub

If you are using the same Button Event to close the Form as well, just add the "Form Close" code last.

codeorder
Posting Virtuoso
1,913 posts since Aug 2010
Reputation Points: 255
Solved Threads: 384
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: