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.

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

Thanks

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.

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

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

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

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.