Click here to Skip to main content
15,906,097 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi I want code to navigate through control using arrow key.
Can anyone help me out?
Posted
Comments
Ankit Rajput 2-May-11 2:32am    
Where you want to navigate?

I do not recommend it as the user normally expects standard behavior.

You need to put all your controls in some data graphs determine which control is a left/right/up/down member of which. You can code this data manually or automatically based calculated position of each control on screen (pretty complex logic). After that, you need to add an event KeyDown to each control automatically. You can use recursion starting from the form/window to traverse the full control set or the graph built on the previous step. This handler can find the direction of navigation based on the button pressed (left/right/up/down) lookup the graph to find out which control is the neighboring in required direction. The result can be null, so no action is required — the current focus control is already leftmost, rightmost, etc. When the found neighboring control is not null, call it's method Focus.

That's it.

Again: I do not recommend it as the user normally expects standard behavior.

—SA
 
Share this answer
 
Comments
Ankur\m/ 2-May-11 3:09am    
That explains the concept, 5!
Sergey Alexandrovich Kryukov 2-May-11 3:17am    
Thank you, Ankur.
--SA
If I understand your question right,
you just need to set correct tab order[^] and you can navigate through the controls using the arrow keys in that order.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 2-May-11 2:50am    
No, it will use Tab, that's the problem. Arrow navigation is easy to implement, I would not recommend it.
--SA
Ankur\m/ 2-May-11 2:53am    
You can also navigate through the arrow keys in the specified tab order. Or am I missing something?
Sergey Alexandrovich Kryukov 2-May-11 3:02am    
Well, how? Tab order is linear (only two directions), but arrow keys assumes 4-directional relationship (left/right/up/down), otherwise it does not worth the effort. Also, tab behavior does not expose a method to find out the neighboring control.

OK, see my answer.
--SA
Ankur\m/ 2-May-11 3:08am    
Okay, got it. Thanks!
parul@123 2-May-11 5:36am    
Thank you all for your answer.
I got the solution.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900