Disabling navigating components via arrows

Budje

Member
Joined
Nov 10, 2006
Messages
6
Programming Experience
1-3
Hi all,

I made a photodisplay application.
When I use the right and left arrows on my keyboard I show the next/previous photo.
The problem is that when I press these arrow keys, the next component on my form get's the focus.
Since I have comboboxes on my form, this means the next value of the combobox get's selected;

I really want to keep using the arrow keys but I need a way to turn of the fact that other components on my form get selected when doing so.

Is there a way to disable component navigation?

Greetz,
 
It's not default behaviour that Left or Right keys changes the focus between controls. There must be some code which does that.

But it's true that if the combobox already has the focus then left/right selects the previous/next value.

Can you show us the code where you check if Left or Right is pressed? and where you put it?
 
Hi CygNuS,

I just created a new form and add 2 buttons and a listbox
When i press the right button on my keyboard the second button on my form gets the focus, when I press it again the listbox had the focus and from then on the items in the listbox get selected when pressing it again.
I didn't program anything yet and it still does that.

Anyway here is the code that checks if left or right is selected:

Dim args As System.EventArgs = New System.EventArgs()
If e.KeyCode = Keys.Right Then
If ButtonNext.Enabled Then
'show next picture via buttonclickevent
ButtonNext_Click(Me, args)
End If
Else
If e.KeyCode = Keys.Left Then
If Buttonprevious.Enabled Then
'show previous picture via buttonclickevent
Buttonprevious_Click(Me, args)
End If
End If
End If
 
Okay i think i know what's happening
I just need to know where this code is located?
 
It's on the keydown event of my form(mybase).
I also have an exact copy on all the PreviewKeyDown events of all my buttons, comboboxes...(I did this because the form doesn't trigger it because it looses focus when navigating).

Greetz

 
This is what's happening:
I suppose the focus is on buttonPrevious to start with, right?
So, Right key is pressed, you trap it and you fire ButtonNext_Click, so that will show the next picture.

The right key does indeed change the focus to the next control by default when it's on a button.

So right after the click event, you should tell the form NOT to do anything by setting e.handled to True

After:
ButtonNext_Click(Me, args)
Insert this:
e.handled = true

same thing with ButtonPrevious

I haven't tested though.
 
Nice, but you can get rid of lots of code by the following:

Try this:
put all your keydown and previewkeydown events in comment
and insert this one, it can catch ALL keydowns no matter what control has focus: (this has proven to be a VERY helpful method to me)

VB.NET:
Protected Overrides Function ProcessCmdKey(ByRef msg As System.Windows.Forms.Message, ByVal keyData As System.Windows.Forms.Keys) As Boolean
   Dim args As System.EventArgs = New System.EventArgs()
   If keyData = Keys.Right Then
        If ButtonNext.Enabled Then
        'show next picture via buttonclickevent
        ButtonNext_Click(Me, args)
        Return True
   End If
   ElseIf keyData = Keys.Left Then
   If ButtonPrevious.Enabled Then
     'show previous picture via buttonclickevent
     ButtonPrevious_Click(Me, args)
     Return True
   End If
   Else
       Return MyBase.ProcessCmdKey(msg, keyData) 'lets the form process further info 
   End If
End Function
 
hmm,

When using this code.
The combobox (where I need to select a value in in order to see my first picture), selected the next/previous item again, which it didn't do with the e.handled=true functionality.

Greetz
 
That's weird, i tested with no keydown/previewkeydown functions at all, just the one function Processcmdkey that i posted and the 2 buttonclick events, and it did what it was supposed to, even with the focus on the combobox.

Anyway, it doesn't matter.
You can use Processcmdkey in the future if you ever need to check for keydowns later on.
 
Back
Top