user1777733 user1777733 - 7 months ago 29
Vb.net Question

How can I call keyDown event by passing aruguments, Winforms Vb.net

Following one is combo box keydown event

Private Sub ComboBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles ComboBox1.KeyDown
If e.KeyCode = Keys.Enter Then
TextBox2.Text = ComboBox1.Text
TextBox2.Focus()
End If
End Sub


I would like to trigger same event from combo box leave by passing 'enter key' I did as follows but not working, how to achieve this?

Private Sub ComboBox1_Leave(sender As Object, e As EventArgs) Handles ComboBox1.Leave
ComboBox1_KeyDown(Me, Keys.Enter)
End Sub

Answer

Why not just extract the method from the actual event?

Private Sub ComboBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles ComboBox1.KeyDown
    performAction(e.KeyCode);
End Sub

Private Sub performAction(e as Keys)
    If e = Keys.Enter Then
        TextBox2.Text = ComboBox1.Text
        TextBox2.Focus()
    End If
End Sub

Private Sub ComboBox1_Leave(sender As Object, e As EventArgs) Handles ComboBox1.Leave
    performAction(Keys.Enter);
End Sub
Comments