This is easy to fix. What I really, really like is that you don't want to use mouse. This is serious talking. Real UI should be able to navigate fully without a mouse. Too bad many applications fail to follow this simple rule -- my respect, Yatin.
First of all, pay attention you kind of asking for the impossible. (I know this is not your real intention, but formally...). Let's see. Consider you focus on
TextBox
. The key Left, from the stand point of text editor should shift to the left character, from the standpoint of your intended form navigation, it should shift focus. It cannot work this way, agree.
First, give up
NavigateControl
at all, in all cases. See below. There can be different resolutions.
#1. Give up this navigation -- it is non-standard, will confuse the user. Seriously.
#2. Put all your controls in Data Grid View -- it will provide similar navigation; contra: may not fit your design, will look to exotic to some users.
#3. Agree to use Ctrl+Right for navigation, Right -- for normal control handling; Ctrl+Up and Up, etc. See explanation of the technique below.
Implementation for last solution:
enum Direction
{ Left, Top, Right, Left, Down, Next, Prev, }
void Navigate(Control current, Direction direction) {}
Direction Translate(KeyboardEventArgs event) {}
void ProcessEvent(KeyboardEventArgs event) {
Direction direction = Translate(event);
switch (direction) {
newFocusControl.Focus();
}
You implementation of
ProcessEvent
should consider keys with prefix (I suggested Ctrl+) to isolate navigation from normal control keyboard processing.
Now, for all controls, individually recursively and programmatically, setup keyboard event handlers:
void SetupKeyboardHandler(control target) {
target.KeyDown += delegate(
object sender, KeyboardEventArgs eventArgs) {
ProcessEvent(eventArgs);
}
target.KeyUp += delegate(
object sender, KeyboardEventArgs eventArgs) {
ProcessEvent(eventArgs);
}
}
static void SetupKeyboardHandlers(Form form) {
foreach(Control control in ControlSet)
SetupKeyboardHandlder(control);
}
Here, I simplified
SetupKeyboardHandlers
. In real life you should collect all controls on the form recursively (using
Control.Controls
property). For simplification, I assume you collected all of them in some collection
ControlSet
, which is not optimal (who cares though?). Better solution should make recursive function and do every
SetupKeyboardHandlder
call directly insider recursion.
This is not too hard. However, I would personally have chosen solution #1.