Arrow keys to navigate through a set of buttons?

N

Neil Wallace

Hi there,

I have an application in which a grid of 100 or more buttons are put on a
form in columns of 10.
All the buttons are within a panel.

They are added in runtime, and so they adopt a sensible tab value.
The tab key moves the focus down the column one by one, and the up and down
arrow keys work well.

What I would like to do, however, is allow the user to use the right/left
arrow keys to jump a column (i.e. current tab value +/- 10), and perhaps the
page down/up keys to move by +/- 50.
Presently these keys do the same as the tab key.

Help!

Cheers

Neil
VB.net Newbie
 
C

Chris, Master of All Things Insignificant

Here's an idea. Not tested by any means, but...

Hold a hashtable of all your buttons as you add them at runtime. Add the
tabindex as the key and the control as the button. Then capture the
keypress event and figure out if you need to handle the arrow keys. If the
key pressed is an arrow look at the tabindex of the control, figure out what
the tabindex of the control you want to move to is. Look up the new
tabindex in the hashtable to find the control. Then set the focus onto the
control that is returned from the hashtable.

Something along these lines: (I don't have an editor to test w/ right now)

Dim HT as new HashTable

For ii as integer = 0 to 100
Dim Btn as New Button
'Setup the button and add to form
HT.Item.Add(btn.tabindex, btn)
AddHandler Btn.KeyDown, Address Btn_KeyDownEvent
Next

Private Sub Btn_KeyDownEvent(sender as object, e as Sometypeofeventhandler)
'Check for the key that was press, I think it is e.key or something like
that
dim ButtonPressed as Button = DirectCast(sender, Button)
dim NewIndex as Integer
'if key was right arrow then.....
NewIndex = ButtonPressed.TabIndex +50
'Do checking to make sure it's not too low or too high
dim NewButton as Button = directcast(HT.item(NewIndex), Button)
NewButton.Focus
End Sub

This code is far from perfect, but it should get you going in the right
direction.

good luck
Chris
 
N

Neil Wallace

Thanks Chris,
I'll give it a go.

Neil

Chris said:
Here's an idea. Not tested by any means, but...

Hold a hashtable of all your buttons as you add them at runtime. Add
the tabindex as the key and the control as the button. Then capture
the keypress event and figure out if you need to handle the arrow
keys. If the key pressed is an arrow look at the tabindex of the
control, figure out what the tabindex of the control you want to move
to is. Look up the new tabindex in the hashtable to find the
control. Then set the focus onto the control that is returned from
the hashtable.
Something along these lines: (I don't have an editor to test w/
right now)
Dim HT as new HashTable

For ii as integer = 0 to 100
Dim Btn as New Button
'Setup the button and add to form
HT.Item.Add(btn.tabindex, btn)
AddHandler Btn.KeyDown, Address Btn_KeyDownEvent
Next

Private Sub Btn_KeyDownEvent(sender as object, e as
Sometypeofeventhandler) 'Check for the key that was press, I think
it is e.key or something like that
dim ButtonPressed as Button = DirectCast(sender, Button)
dim NewIndex as Integer
'if key was right arrow then.....
NewIndex = ButtonPressed.TabIndex +50
'Do checking to make sure it's not too low or too high
dim NewButton as Button = directcast(HT.item(NewIndex), Button)
NewButton.Focus
End Sub

This code is far from perfect, but it should get you going in the
right direction.

good luck
Chris
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top