I have defined a procedure under the click event of the button as follows.

Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click
        'Validate Department
        If cmbDepartment.SelectedItem = Nothing Then
            MsgBox("Please select a department", MsgBoxStyle.Information, "Notification")
            cmbDepartment.Focus()
            Exit Sub
        End If
End Sub

I want to call this procedure when press on the enter key


Private Sub txtSearchValue_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtSearchValue.KeyPress
If Asc(e.KeyChar) = 13 Then

MsgBox("Run the btnSearch click event")


End If
End sub


Can anyone suggest me a way to do this???????
Would be a great help

Dani AI

Generated

Two clean ways to trigger the same logic from both the button and the Enter key are: (1) move the work into a shared method and call it, and (2) let the form route Enter to the button via AcceptButton. Avoid calling one event handler from another; event handlers should delegate to a method you own.

' In your form
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Me.AcceptButton = btnSearch   ' Pressing Enter in most controls clicks btnSearch
End Sub

Private Sub DoSearch()
    ' Validate first
    If cmbDepartment.SelectedIndex = -1 Then
        MessageBox.Show("Please select a department.", "Notification",
                        MessageBoxButtons.OK, MessageBoxIcon.Information)
        cmbDepartment.Focus()
        Exit Sub
    End If

    ' ... your search logic here ...
End Sub

Private Sub btnSearch_Click(sender As Object, e As EventArgs) Handles btnSearch.Click
    DoSearch()
End Sub

Private Sub txtSearchValue_KeyDown(sender As Object, e As KeyEventArgs) Handles txtSearchValue.KeyDown
    If e.KeyCode = Keys.Enter Then
        e.SuppressKeyPress = True   ' prevent ding and newline
        DoSearch()                  ' or: btnSearch.PerformClick()
    End If
End Sub

Notes:

  • AcceptButton will not fire when a multiline TextBox with AcceptsReturn = True has focus; use the KeyDown approach in that case.
  • Prefer SelectedIndex = -1 over SelectedItem Is Nothing for ComboBox validation in WinForms.

Recommended Answers

All 4 Replies

Please use code tags in the future

<- Remove the spaces and it will work
code here

To answer your question you should move the functionality to another method instead of calling the button's click event, but:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
		Button2_Click(sender, New System.EventArgs())
	End Sub

	Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
		MessageBox.Show("I called button2 from button1")
	End Sub

Thank you very much.
Awesome. Working exactly the way I want.
Thanks again

thaks ya

That was cool.. it works but when i call button click event
it does not do all things on the button1 event. for example

button1_click
msgbox "Boom"
Call loadmyData 'this thing didnt fire

button2_click
button1_click(sender, new system.eventargs())

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.