0

I have a C# Windows Form Application that uses 10 textboxes as input fields, and I would like to be able to program these so that whichever textbox has focus can have the up arrow (Keys.Up) or down arrow (Keys.Down) can be pressed and focus will jump to the next or previous textbox.

So far I plan to use something like this:

        if (e.KeyChar == Convert.ToChar(Keys.Up))
        {
            GetNextControl((TextBox)sender, false);
        }
        else if (e.KeyChar == Convert.ToChar(Keys.Down))
        {
            GetNextControl((TextBox)sender, true);
        }

My only concern is whether or not this will interfere with the entry of actual text. Would the code above need to be changed to something like the code below?

        if (e.KeyChar == Convert.ToChar(Keys.Up))
        {
            GetNextControl((TextBox)sender, false);
        }
        else if (e.KeyChar == Convert.ToChar(Keys.Down))
        {
            GetNextControl((TextBox)sender, true);
        }
        //any other key pressed
        else
        {
            TextBox input = (TextBox)sender;
            //add char relating to pressed key to text in TextBox
            input.AppendText(e.KeyChar.ToString());
        }

Is this else clause required or will the default operations of the TextBox handle this condition?

Thanks, Mark

marcuthh
  • 592
  • 3
  • 16
  • 42
  • 3
    When in doubt, make a small test project and find out .. – BugFinder May 25 '16 at 11:41
  • I did think about this, what I was hoping from posting on here was to be told whether I was taking the best approach to this or if there was some existing functionality I didn't know about @BugFinder – marcuthh May 25 '16 at 11:44
  • Messing with the default handling of focus between control is rarely a good idea to start with and disconcerting for users' habits – Steve May 25 '16 at 11:46
  • 1
    You might want to look at [Up, Down, Left and Right arrow keys do not trigger KeyDown event](http://stackoverflow.com/questions/1646998/up-down-left-and-right-arrow-keys-do-not-trigger-keydown-event) – stuartd May 25 '16 at 11:48
  • @Steve, why in this case it's a problem? Up/down keys aren't used by `TextBox` (unless it's multiline TextBox) and adding to default tab/shift+tab navigation arrow keys is a plus for user experience, I'd even add automatic focus switch to next control on enter. – Sinatr May 25 '16 at 11:50
  • Right, but when you tab to a filled TextBox the text is highlighted and I, for example, have the habit to press the DOWN key to remove the highlight and start to modify the text from the end or the UP key to edit from the start. As you can see you could surprise someone with these changes and while you add a new (unknown) functionality you remove a (known) functionality. From my point of view it is not recommended, but I am the last one to have a say in this. – Steve May 25 '16 at 13:20

2 Answers2

1

What is the e.KeyChar for Up key? Do not use char, but key codes:

private void myTextBoxes_KeyDown(object sender, KeyEventArgs e) {
  // KeyCode: there're no reasonable chars after "Up" or "Down" keys
  if (e.KeyCode == Keys.Up) {
    e.Handled = true; // to prevent system processing

    // (Control): what if you want to add, say, RichEdit into the pattern? 
    GetNextControl((Control) sender, false);
  }
  else if (e.KeyCode == Keys.Down) {
    e.Handled = true; // to prevent system processing

    GetNextControl((Control) sender, true);
  }
}
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
1

Sounds like a good plan. For your example it will work.

To make it a bit more generic (not only for text boxes) may I recommend reading about the PreviewKeyDown event (rationale: in some controls up/down keys will never fire the KeyDown event, though it will work for text box).

As for GetNextControl it may be benefitial to use FindForm().SelectNextControl() instead as it will allow more granular control over what gets skipped.

p.s. Also ... GetNextControl will return the control. But will not jump to it. You'll have to add .Focus() to it.

Tomaz Stih
  • 529
  • 3
  • 10