Controling a group of Buttons using Arrow Keys

akashdeep

Member
Joined
Jul 17, 2013
Messages
7
Programming Experience
3-5
Hi, I'm new to Vb.net programming.
I designed a simple form in which I put a group of buttons in a Panel. The panel is initially not Enabled.
I added a Start button which Enabled the Panel. Next, I want to use the Arrow Keys to navigate between the buttons in the Panel. I have tried searching about this on the web but nothing seems to work. See the screenshot.
Please help.
P.S.: I am using Visual Studio 2012 Express and targeting .NET Framework 3.5 SP1.
Capture.JPG
 
Okay, I got a partial solution by digging deep in the forum. Here's the code:
VB.NET:
 Protected Overrides Function ProcessDialogKey(ByVal keyData As System.Windows.Forms.Keys) As Boolean
        Select Case keyData
            Case Keys.Left
                Select Case Me.ActiveControl.Name
                    Case "Button1"
                        Button2.Focus()
                    Case "Button2"
                        Button1.Focus()
                    Case "Button3"
                        Button4.Focus()
                    Case "Button4"
                        Button3.Focus()
                    Case Else
                        Return False
                End Select
            Case Keys.Right
                Select Case Me.ActiveControl.Name
                    Case "Button1"
                        Button2.Focus()
                    Case "Button2"
                        Button1.Focus()
                    Case "Button3"
                        Button4.Focus()
                    Case "Button4"
                        Button3.Focus()
                    Case Else
                        Return False
                End Select
            Case Keys.Up
                Select Case Me.ActiveControl.Name
                    Case "Button1"
                        Button3.Focus()
                    Case "Button2"
                        Button4.Focus()
                    Case "Button3"
                        Button1.Focus()
                    Case "Button4"
                        Button2.Focus()
                    Case Else
                        Return False
                End Select
            Case Keys.Down
                Select Case Me.ActiveControl.Name
                    Case "Button1"
                        Button3.Focus()
                    Case "Button2"
                        Button4.Focus()
                    Case "Button3"
                        Button1.Focus()
                    Case "Button4"
                        Button2.Focus()
                    Case Else
                        Return False
                End Select
            Case Else
                Return False
        End Select
        Return False
    End Function

But, I'm faced with a new problem. I need the Tab to work to cycle through controls as usual. Due to the ProcessDialogKey Function, the Tab function doesn't work. I changed the layout a bit. Now the form loads into a TextBox. Ideally, pressing Tab should shift focus to the START button as it is next in terms of TabIndex. But because of ProcessDialogKey, it does not.

Is there a way to keep Tab key in ProcessDialogKey?

Can anyone please suggest a way out? Thanks!
Capture.JPG
 

Latest posts

Back
Top