Click here to Skip to main content
15,922,894 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I am woking on a funtion which will be used for navigating between controls by Up/Left/Right/Down arrow keys and on enter key. my code is like below.
public void NavigateControl(Form form, KeyEventArgs e)
{           
  Control actCntrl = form.ActiveControl;
  if (actCntrl.GetType() == typeof(TextBox))
  {
    TextBox tb = actCntrl as TextBox;
    Control c = tb.Parent;
    if (e.KeyCode == Keys.Right || e.KeyCode == Keys.Down)
    {
      Control next = c.GetNextControl(tb, true);
      if (next != null)
        next.Focus();
    }
    else if (e.KeyCode == Keys.Left || e.KeyCode == Keys.Up)
    {
      Control next = c.GetNextControl(tb, false);
      if (next != null)
        next.Focus();
    }
  }
}

It is working. But I have a little problem with this. Suppose in a textbox I have text(e.g 'cod project'). Now I have found that there is a character misssing in the text('e' is missing) so I'm trying to move to that position(next to the d) by right key, but as per my code on right key down cursor moves to next control. So I can't move within text of textbox on any arrow key.... I don't want to use the mouse. The same thing is happening with Combobox, datetimepicker etc..
What to do to remove this problem?
Please help me.
Posted
Updated 13-Jan-11 20:34pm
v2
Comments
JF2015 14-Jan-11 2:34am    
Edited to add code formatting.

I can see what you are trying to do, and unfortunately, this is the kind of problem you get when you try to twist the normal UI experience into something the user is not going to expect: they expect the left and right keys to move left and right within a text box, as you have seen.

There are a few things you can do:
1) Insist that the navigation controls are only active when the CTRL key is pressed. This is less confusing for the user because the cursor keys now do the same in all applications.
2) Make the CTRL key have the old, normal functionality with teh cursor keys. Horrible for the user, and not a lot of fun for you.

You do realise that you can use TAB and CTRL+TAB to move between input fields in a defined order, don't you? And that you can assign short-cut keys to input controls (so the user types ALT+N for the "Name" field, ALT+P for the "Position" and so on)?
These are at least common to the entire UI experience, so users wouldn't get confused.
 
Share this answer
 
Comments
Espen Harlinn 14-Jan-11 3:23am    
5+ for the last paragraph
JF2015 14-Jan-11 5:33am    
Last paragraph is a good hint - I too tend to forget this :(
Sergey Alexandrovich Kryukov 14-Jan-11 11:11am    
I am fully agree -- this is design and usability misconception (my 5).
See also my comment to my answer.
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:

//make universal navigation handler:
enum Direction
   { Left, Top, Right, Left, Down, Next, Prev, } //whatever

//...

void Navigate(Control current, Direction direction) {/* ... */}
Direction Translate(KeyboardEventArgs event)  {/* ... */}
void ProcessEvent(KeyboardEventArgs event) {
    Direction direction = Translate(event);
    switch (direction) {
    //...
    newFocusControl.Focus(); //something like this after calculations
}


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.KeyDown
    target.KeyUp += delegate(
        object sender, KeyboardEventArgs eventArgs) {
            ProcessEvent(eventArgs);
    } //target.KeyDown
} //SetupKeyboardHandler

static void SetupKeyboardHandlers(Form form) {
    //...
    foreach(Control control in ControlSet)
        SetupKeyboardHandlder(control);
} //SetupKeyboardHandlers


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.
 
Share this answer
 
Comments
Espen Harlinn 14-Jan-11 3:22am    
5+ Nicely written answer
JF2015 14-Jan-11 5:32am    
Good answer. 5+
Sergey Alexandrovich Kryukov 14-Jan-11 11:10am    
@Yatin: Maybe, the root of your problem is different: you have to many controls at a time in a single form. Then you need to add a level of structure to the UI desing: Tab control, master-detail control (master: tree view or list box, detail: separate control panel per list/tree item, all other panels are hidden), something else like that...
Yatin Bhagat 17-Jan-11 7:48am    
My client uses a Dos based software ..which is much easy to navigate with out mouse....and client have a habbit of using enter keys or arrow keys..thats why for my dot net application very first requirment is Software with less use of Mouse...and I have to do this....i know very well tab or Shift+tab is available or i can arrange some Combination key navigation like Cntrol+ A etc....But i need to do this with Enter Keys and Arrow keys.
Sergey Alexandrovich Kryukov 18-Jan-11 0:05am    
Again, I supported the idea that everything should be controllable with keyboard, not just mouse. I explained how to process the keyboard and two options. A tab (Tab and Shift+Tab for reverse) is a standard navigation key, should be preserved, Enter or Arrow keys -- no difference. I demonstrated the principle, anyway.

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