KeyPreview - How do I overcome this problem?

Cheetah

Well-known member
Joined
Oct 12, 2006
Messages
232
Programming Experience
Beginner
Hi there,

I have keypreview enabled on my form but the events are not registering for the arrow keys.

This is because a button is focused in a flow panel layout, so it just cycles through the buttons in that flow panel layout.

How do I overcome this problem?

Thanks.
 

Attachments

  • WindowsApplication3.zip
    10.4 KB · Views: 11
Last edited by a moderator:
This may be the right place for this:
VB.NET:
Protected Overrides Function ProcessDialogKey(ByVal keyData As System.Windows.Forms.Keys) As Boolean
    Select Case keyData
        Case Keys.Left, Keys.Right, Keys.Up, Keys.Down
            Me.Text = keyData.ToString
    End Select
    Return MyBase.ProcessDialogKey(keyData)
End Function
Probably more than you want to know about keyboarding in Windows Forms….
 
Thanks - that doesn't trigger the keydown event, my i get the picture, I can put the appropriate logic where i need.

I think i understand what it is doing, but I am a bit confused by this:

VB.NET:
Return MyBase.ProcessDialogKey(keyData)

What exactly is that doing?

Thanks.
 
ProcessDialogKey overrides the default base method. Calling the base method passes the information on to the base class for normal processing flow of the event once you've done your business. This is standard class design and this call is inserted automatically by the code editor when you override such methods. In some cases you change parameters before passing it on to change the outcome of the event (for example cancel some cases or set other flags).
 
For some reason, after i integrated this into my application it doesn't seem to work.

It only triggers when the focus is on the form, but when i tested it on a temporary application, it triggered when a button was focussed aswell

Could it be due to the fact that the buttons are being added at runtime instead of design time in my main application?
 
Last edited by a moderator:
I removed the C# project attachment, feel free to post a VB.Net project we may help you with here.
Also remember to remove the compiled binaries folders (Obj + Bin) before you upload.
 
Back
Top