Feedback Form

Tuesday, September 25, 2007

Move controls using Arrow keys

I faced a problem today while trying to move a custom control I did on a windows form using keyboard arrow keys, the problem that the KeyDown event don't feel arrow keys, I read about some and found some articles about.
Some articles were not good at all but it may be a very fast solution and may help others but it didn't help me :)
I read for example that you can use the KeyUp event instead; it will work but suppose you need to move an object using KeyUp, ooh, very annoying and not user friendly.
But the best fastest solution I found was the following:

Protected Overrides Function ProcessCmdKey(ByRef msg As Message, ByVal keyData As Keys) As Boolean
'Returns true if the character was processed by the control;
'otherwise, false.

Dim bHandled As Boolean = False
Select Case keyData
Case Keys.Right
Me.currentControl.Left += 10
bHandled = True
Case Keys.Left
Me.currentControl.Left -= 10
bHandled = True
Case Keys.Up
Me.currentControl.Top -= 10
bHandled = True
Case Keys.Down
Me.currentControl.Top += 10
bHandled = True
End Select
Return bHandled
End Function


kick it on