emi_sastra
asked on
BUTTON AND KEYCODE
Hi All,
I have a windows form.
I let the user to use mouse click or keystroke from keyboard to do something.
Every button has associate keystroke.
For example Button Ok equal to Key F4.
I have below code to check keystroke :
Private Sub frmMain_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
If e.KeyCode = Keys.Escape Then
Dim intAnswer As Integer = MsgBox("Yakin Mau Keluar Dari Menu ...!", MsgBoxStyle.YesNo, Me.Text)
If intAnswer = vbYes Then
Me.Close()
Exit Sub
End If
End If
Me.Call_Form(sender, e)
End Sub
Private Sub Call_Form(sender As Object, e As KeyEventArgs)
Select Case e.KeyCode
Case Keys.F3
Dim frmPanggilPesanan As New frmPanggilPesanan
With frmPanggilPesanan
.ShowDialog()
End With
frmPanggilPesanan = Nothing
Case Keys.F5
Dim frmTundaPesanan As New frmTundaPesanan
With frmTundaPesanan
.ShowDialog()
End With
frmTundaPesanan = Nothing
Case Keys.F6
Dim frmDaftarPesanAntar As New frmDaftarPesanAntar
With frmDaftarPesanAntar
.ShowDialog()
End With
frmDaftarPesanAntar = Nothing
Case Keys.F9
Dim frmPembayaran As New frmPembayaran
With frmPembayaran
.ShowDialog()
End With
frmPembayaran = Nothing
Case Keys.F11
Dim frmMenuKasir As New frmMenuKasir
With frmMenuKasir
.ShowDialog()
End With
frmMenuKasir = Nothing
Case Keys.F12
Dim intYesNo As Integer = MsgBox("Abaikan Perubahan ...!", MsgBoxStyle.YesNo, Me.Text)
If intYesNo = vbYes Then
Clear_Grid()
End If
End Select
End Sub
I want to use sub Call_Form from button click. I don't want to make duplicate sub for check button.
How could I do it ?
Thank you.
I have a windows form.
I let the user to use mouse click or keystroke from keyboard to do something.
Every button has associate keystroke.
For example Button Ok equal to Key F4.
I have below code to check keystroke :
Private Sub frmMain_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
If e.KeyCode = Keys.Escape Then
Dim intAnswer As Integer = MsgBox("Yakin Mau Keluar Dari Menu ...!", MsgBoxStyle.YesNo, Me.Text)
If intAnswer = vbYes Then
Me.Close()
Exit Sub
End If
End If
Me.Call_Form(sender, e)
End Sub
Private Sub Call_Form(sender As Object, e As KeyEventArgs)
Select Case e.KeyCode
Case Keys.F3
Dim frmPanggilPesanan As New frmPanggilPesanan
With frmPanggilPesanan
.ShowDialog()
End With
frmPanggilPesanan = Nothing
Case Keys.F5
Dim frmTundaPesanan As New frmTundaPesanan
With frmTundaPesanan
.ShowDialog()
End With
frmTundaPesanan = Nothing
Case Keys.F6
Dim frmDaftarPesanAntar As New frmDaftarPesanAntar
With frmDaftarPesanAntar
.ShowDialog()
End With
frmDaftarPesanAntar = Nothing
Case Keys.F9
Dim frmPembayaran As New frmPembayaran
With frmPembayaran
.ShowDialog()
End With
frmPembayaran = Nothing
Case Keys.F11
Dim frmMenuKasir As New frmMenuKasir
With frmMenuKasir
.ShowDialog()
End With
frmMenuKasir = Nothing
Case Keys.F12
Dim intYesNo As Integer = MsgBox("Abaikan Perubahan ...!", MsgBoxStyle.YesNo, Me.Text)
If intYesNo = vbYes Then
Clear_Grid()
End If
End Select
End Sub
I want to use sub Call_Form from button click. I don't want to make duplicate sub for check button.
How could I do it ?
Thank you.
ASKER
-should be equivalent to specific keystroke?
Yes.
Thank you.
Yes.
Thank you.
ASKER
I want to reuse Sub Call_Form(sender As Object, e As KeyEventArgs) for button click.
Thank you.
Thank you.
which keystroke?
F4 key?
F4 key?
ASKER
-which keystroke?
Every button has associate keystroke.
Thank you.
Every button has associate keystroke.
Thank you.
change Keys.F2 to the button associated key, and change button1 to the button name.
in your button click event handler, have this code:
in your button click event handler, have this code:
Dim keyEventArgs = New KeyEventArgs(Keys.F2)
Call_Form(button1, keyEventArgs)
ASKER
Suppose I have 10 button.
How to code it ?
Thank you.
How to code it ?
Thank you.
ASKER
For example :
btnAdd F2
btnUpd F3
btnDel F4
btnSave F5
btnCancel F6
Thank you.
btnAdd F2
btnUpd F3
btnDel F4
btnSave F5
btnCancel F6
Thank you.
add click event handler for each button and inside pass the relevant key code to Call_Form.
for example:
for example:
sub btnAdd_Click(...)
Dim keyEventArgs = New KeyEventArgs(Keys.F2)
Call_Form(btnAdd, keyEventArgs)
end sub
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Really great.
Thank you very much for your help.
Thank you very much for your help.
ASKER
One problem.
How if the key stroke is Ctrl+Del or other combination key ?
Thank you.
How if the key stroke is Ctrl+Del or other combination key ?
Thank you.
u use the bitwise OR (|) operator to apply modifier like Ctrl/Alt keys.
for example:
for example:
Dim keyEventArgs = New KeyEventArgs(Keys.Delete Or Keys.Control)
ASKER
Below code error :
dictionary.Add(btnCancel, (Keys.Delete Or Keys.Control))
Thank you.
dictionary.Add(btnCancel, (Keys.Delete Or Keys.Control))
Thank you.
what error?
ASKER
Error 1 Constant expression not representable in type 'Short'.
Thank you.
Thank you.
change Short to Integer
ASKER
No compile error.
Thank you very much for your help.
Thank you very much for your help.
u welcome.
to do what?
should be equivalent to specific keystroke?