?
Solved

BUTTON AND KEYCODE

Posted on 2013-06-16
19
Medium Priority
?
268 Views
Last Modified: 2013-06-17
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.
0
Comment
Question by:emi_sastra
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 10
  • 9
19 Comments
 
LVL 42

Expert Comment

by:sedgwick
ID: 39252344
>>I want to use sub Call_Form from button click
to do what?
should be equivalent to specific keystroke?
0
 
LVL 1

Author Comment

by:emi_sastra
ID: 39252364
-should be equivalent to specific keystroke?
Yes.

Thank you.
0
 
LVL 1

Author Comment

by:emi_sastra
ID: 39252365
I want to reuse Sub Call_Form(sender As Object, e As KeyEventArgs) for button click.

Thank you.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 42

Expert Comment

by:sedgwick
ID: 39252423
which keystroke?
F4 key?
0
 
LVL 1

Author Comment

by:emi_sastra
ID: 39252451
-which keystroke?
Every button has associate keystroke.

Thank you.
0
 
LVL 42

Expert Comment

by:sedgwick
ID: 39252466
change Keys.F2 to the button associated key, and change button1 to the button name.
in your button click event handler, have this code:
Dim keyEventArgs = New KeyEventArgs(Keys.F2)
Call_Form(button1, keyEventArgs)

Open in new window

0
 
LVL 1

Author Comment

by:emi_sastra
ID: 39252471
Suppose I have 10 button.
How to code it ?

Thank you.
0
 
LVL 1

Author Comment

by:emi_sastra
ID: 39252473
For example :

btnAdd        F2
btnUpd        F3
btnDel         F4
btnSave       F5
btnCancel   F6

Thank you.
0
 
LVL 42

Expert Comment

by:sedgwick
ID: 39252505
add click event handler for each button and inside pass the relevant key code to Call_Form.
for example:
sub btnAdd_Click(...)
Dim keyEventArgs = New KeyEventArgs(Keys.F2)
Call_Form(btnAdd, keyEventArgs)
end sub

Open in new window

0
 
LVL 42

Accepted Solution

by:
sedgwick earned 2000 total points
ID: 39252509
you can create a single click event handler for all buttons and pass relevant key code to Call_Form, instead of creating click event handler for each one.
to do so u need first to map each button to its associated key using dictionary or hash, and then create a single handler to handle all buttons click events:
    Private Sub genericButtonHandler_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click, btnSave.Click, btnCancel.Click,btnDel.Click,btnUpd.Click
        Dim dictionary As New Dictionary(Of Button, Short)
        dictionary.Add(btnAdd, Keys.F2)
        dictionary.Add(btnUpd, Keys.F3)
        dictionary.Add(btnDel, Keys.F4)
        dictionary.Add(btnSave, Keys.F5)
        dictionary.Add(btnCancel, Keys.F6)
        Dim btn As Button = DirectCast(sender, Button)
        Dim keyEventArgs = New KeyEventArgs(dictionary(btn))
Call_Form(btn, keyEventArgs)
    End Sub

Open in new window

0
 
LVL 1

Author Closing Comment

by:emi_sastra
ID: 39252640
Really great.

Thank you very much for your help.
0
 
LVL 1

Author Comment

by:emi_sastra
ID: 39252648
One problem.

How if the key stroke is Ctrl+Del or other combination key ?

Thank you.
0
 
LVL 42

Expert Comment

by:sedgwick
ID: 39252687
u use the bitwise OR (|) operator to apply modifier like Ctrl/Alt keys.
for example:
Dim keyEventArgs = New KeyEventArgs(Keys.Delete Or Keys.Control)

Open in new window

0
 
LVL 1

Author Comment

by:emi_sastra
ID: 39252989
Below code error :

 dictionary.Add(btnCancel, (Keys.Delete Or Keys.Control))

Thank you.
0
 
LVL 42

Expert Comment

by:sedgwick
ID: 39253088
what error?
0
 
LVL 1

Author Comment

by:emi_sastra
ID: 39253137
Error      1      Constant expression not representable in type 'Short'.      

Thank you.
0
 
LVL 42

Expert Comment

by:sedgwick
ID: 39253147
change Short to Integer
0
 
LVL 1

Author Comment

by:emi_sastra
ID: 39253233
No compile error.

Thank you very much for your help.
0
 
LVL 42

Expert Comment

by:sedgwick
ID: 39253259
u welcome.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Suggested Courses

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question