-
Feb 3rd, 2022, 12:02 PM
#1
Thread Starter
New Member
Non-restricted movement between buttons
Hi there
I am creating a Windows Forms App .NET with VB in VS, and I have 4 buttons arranged in a square (2 rows and 2 columns).
I would like to have free movement between the buttons using the arrow keys, so that when the upper left button has focus
I can move focus to the lower left button with the down arrow, or to the upper right button using the right arrow.
I already sat the focus to be on the upper left button when the form is shown, and I have tinkered a little bit with the
TabIndex and the TabStop, but can't seem to get 100% free movement.
Also, I have tried using a KeyDown event on the button, like this:
If e.KeyCode = Keys.Down Then
Button4.Focus()
End if
But it does not work...
Any ideas?
Thanks!
-
Feb 3rd, 2022, 01:32 PM
#2
Re: Non-restricted movement between buttons
Try this...
Code:
Public Class Form20
Dim buttons(1)() As Button
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
buttons(0) = New Button() {Button1, Button2}
buttons(1) = New Button() {Button3, Button4}
End Sub
Protected Overrides Function ProcessCmdKey(ByRef msg As System.Windows.Forms.Message, ByVal keyData As System.Windows.Forms.Keys) As Boolean
Dim sy As Integer
Dim sx As Integer
sx = Array.IndexOf(buttons(0), Control.FromHandle(msg.HWnd))
If sx > -1 Then
sy = 0
Else
sy = 1
sx = Array.IndexOf(buttons(1), Control.FromHandle(msg.HWnd))
End If
Select Case keyData
Case Keys.Up
If sx = -1 then Return False
If sy = 1 Then
buttons(0)(sx).Select()
End If
Return True
Case Keys.Down
If sy = 0 Then
buttons(1)(sx).Select()
End If
Return True
Case Keys.Left
If sx = 1 Then
buttons(sy)(0).Select()
End If
Return True
Case Keys.Right
If sx = 0 Then
buttons(sy)(1).Select()
End If
Return True
End Select
Return False
End Class
Last edited by .paul.; Feb 3rd, 2022 at 01:52 PM.
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Feb 3rd, 2022, 02:09 PM
#3
Thread Starter
New Member
Re: Non-restricted movement between buttons
Thank you for your reply, I will try it in a moment
And if I had 9 buttons also in a square (3x3) it would just be this?:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
buttons(0) = New Button() {Button1, Button2, Button3}
buttons(1) = New Button() {Button4, Button5, Button6}
buttons(2) = New Button() {Button7, Button8, Button9}
End Sub
And then change the rest accordingly?
-
Feb 3rd, 2022, 02:17 PM
#4
Re: Non-restricted movement between buttons
Originally Posted by Anderskd1
Thank you for your reply, I will try it in a moment
And if I had 9 buttons also in a square (3x3) it would just be this?:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
buttons(0) = New Button() {Button1, Button2, Button3}
buttons(1) = New Button() {Button4, Button5, Button6}
buttons(2) = New Button() {Button7, Button8, Button9}
End Sub
And then change the rest accordingly?
There would be more to do than just changing the array
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Feb 3rd, 2022, 02:21 PM
#5
Thread Starter
New Member
Re: Non-restricted movement between buttons
Would you be so kind to explain the rest of the changes if ther were 3x3 buttons, please?
I'm just not sure that I understand the "sx" and "sy" part.
-
Feb 3rd, 2022, 02:27 PM
#6
Re: Non-restricted movement between buttons
Imagine a square grid with 3 rows and 3 columns
sx is the horizontal index of a cell
sy is the vertical index of a cell
Now you can see that the array is a square grid of 3 rows of 3 buttons
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Feb 3rd, 2022, 02:29 PM
#7
Thread Starter
New Member
Re: Non-restricted movement between buttons
Ah, okay, that makes a lot of sense, but what is up with the "-1"?
-
Feb 3rd, 2022, 02:49 PM
#8
Re: Non-restricted movement between buttons
Here's a 3 by 3 grid of buttons...
Code:
Public Class Form1
Dim buttons(2)() As Button
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
buttons(0) = New Button() {Button1, Button2, Button3}
buttons(1) = New Button() {Button4, Button5, Button6}
buttons(2) = New Button() {Button7, Button8, Button9}
End Sub
Protected Overrides Function ProcessCmdKey(ByRef msg As System.Windows.Forms.Message, ByVal keyData As System.Windows.Forms.Keys) As Boolean
Dim sy As Integer = -1
Dim sx As Integer = -1
sx = Array.IndexOf(buttons(0), Control.FromHandle(msg.HWnd))
If sx > -1 Then
sy = 0
Else
sx = Array.IndexOf(buttons(1), Control.FromHandle(msg.HWnd))
If sx > -1 Then
sy = 1
Else
sx = Array.IndexOf(buttons(2), Control.FromHandle(msg.HWnd))
If sx > -1 Then
sy = 2
Else
sy = -1
End If
End If
End If
Select Case keyData
Case Keys.Up
If sy = 1 Then
buttons(0)(sx).Select()
End If
If sy = 2 Then
buttons(1)(sx).Select()
End If
Return True
Case Keys.Down
If sy = 0 Then
buttons(1)(sx).Select()
End If
If sy = 1 Then
buttons(2)(sx).Select()
End If
Return True
Case Keys.Left
If sx = 1 Then
buttons(sy)(0).Select()
End If
If sx = 2 Then
buttons(sy)(1).Select()
End If
Return True
Case Keys.Right
If sx = 0 Then
buttons(sy)(1).Select()
End If
If sx = 1 Then
buttons(sy)(2).Select()
End If
Return True
End Select
Return False
End Function
End Class
The Array.IndexOf function, either finds the index of what you specify, or -1 if it can't find it...
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Feb 3rd, 2022, 02:53 PM
#9
Re: Non-restricted movement between buttons
Control.FromHandle(msg.HWnd) is the Button (or any control on your form) that has the focus when the CMD key is pressed
Last edited by .paul.; Feb 3rd, 2022 at 03:05 PM.
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Feb 3rd, 2022, 03:05 PM
#10
Re: Non-restricted movement between buttons
To avoid blocking arrow key navigation on other controls on your form, such as ListBoxes, or DataGridViews...
Code:
Protected Overrides Function ProcessCmdKey(ByRef msg As System.Windows.Forms.Message, ByVal keyData As System.Windows.Forms.Keys) As Boolean
Dim sy As Integer = -1
Dim sx As Integer = -1
sx = Array.IndexOf(buttons(0), Control.FromHandle(msg.HWnd))
If sx > -1 Then
sy = 0
Else
sx = Array.IndexOf(buttons(1), Control.FromHandle(msg.HWnd))
If sx > -1 Then
sy = 1
Else
sx = Array.IndexOf(buttons(2), Control.FromHandle(msg.HWnd))
If sx > -1 Then
sy = 2
Else
sy = -1
End If
End If
End If
Select Case keyData
Case Keys.Up
If sy = -1 Then Return False
If sy = 1 Then
buttons(0)(sx).Select()
End If
If sy = 2 Then
buttons(1)(sx).Select()
End If
Return True
Case Keys.Down
If sy = -1 Then Return False
If sy = 0 Then
buttons(1)(sx).Select()
End If
If sy = 1 Then
buttons(2)(sx).Select()
End If
Return True
Case Keys.Left
If sx = -1 Then Return False
If sx = 1 Then
buttons(sy)(0).Select()
End If
If sx = 2 Then
buttons(sy)(1).Select()
End If
Return True
Case Keys.Right
If sx = -1 Then Return False
If sx = 0 Then
buttons(sy)(1).Select()
End If
If sx = 1 Then
buttons(sy)(2).Select()
End If
Return True
End Select
Return False
End Function
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Feb 3rd, 2022, 03:12 PM
#11
Thread Starter
New Member
Re: Non-restricted movement between buttons
Wow thank you very much for your help! I really appreciate it.
I will try it out tomorrow
-
Feb 4th, 2022, 11:22 AM
#12
Thread Starter
New Member
Re: Non-restricted movement between buttons
It worked! Thank you very much, Paul!
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|