Command button shortcut key

  • Thread starter Thread starter mf_sina
  • Start date Start date
M

mf_sina

Hi!

i have a form in VB.Net with two command buttons CmdDelete and CmdAdd
i need to add shortcut keys for example F8 for CmdDelete and F4 for CmdAdd
but i don't have any code.
could someone plz show me a sample code...

Best wishes
 
Hi,

You can't directly associate function keys with command button in .net. To achieve this, you can set the KeyPreview property of the Form to True and
then call the Command button's click event in Form's KeyDown event. Here is the code that would work:

Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown

If e.KeyCode = 115 Then 'For F4 key

Call Button1_Click(Button1, e)

End If

End Sub

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

MessageBox.Show("test")

End Sub

Hope this helps. Thanks

Mona[Grapecity]
 
Back
Top