1

Is it possible to have a textbox ignore Ctrl + character function and reassign a different use for the ctrl key?

Situation: I have multiple text boxes on a Form. Ideally I would like the textboxes to jump from one textbox to another after enter in a single character. However the textbox can have more than one input, in this case I want the user to hold ctrl to stay on the textbox while they enter in multiple characters.

Essentially, I want my ctrl (if held down) to stop the textbox from jumping.

I've tried using a combination of OnKeyDown, OnKeyUp and TextChanged event to handle when the Ctrl key is pressed, but in the end, I can't enter any data once the Ctrl key is held down. Is there a way around this?

The reason for this is because 95 percent of the time its always 1 character. Therefore to quickly traverse through the textboxes, the user would hit a single character. However in the unlikelihood that the user wants to enter in 2 digits, they can hold Ctrl (So it doesn't jump to the next textbox) and enter in 2 digits as desired.

jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
Michael Chi Lam
  • 390
  • 1
  • 13
  • 2
    You can surely somehow convert `Ctrl`+`SomeKey` to char (see [this](http://stackoverflow.com/q/6929275/1997232), etc.), but are you sure in what you are doing? I sounds absolutely uncomfortable (for me) to type while having `Ctrl` pressed. It is unclear what scenario you are trying to implement and why `TextBox` change its focus after entering one character. Can you provide some code perhaps and explain it better? Maybe there is a better solution. – Sinatr Mar 11 '15 at 15:32
  • 2
    The first usability study you perform on your UI should be on your team mates. Hopefully they can speak freely and tell you what they think of it. In the unlikely case that doesn't discourage you then at least pick a key that doesn't alter the character code. Nobody ever has a good use for the ScrollLock key. – Hans Passant Mar 11 '15 at 15:39
  • @ Sinartr - Please see edit for the reason behind this bizarre situation! :) – Michael Chi Lam Mar 11 '15 at 15:52
  • @Hans Passant - The UI team actually requested this. My original design was to allow the user to enter in as many characters they want in the textbox and hit tab or enter to move on to the next Textbox. Good suggestion regarding the ScrollLock key! Thanks :) – Michael Chi Lam Mar 11 '15 at 15:54
  • 1
    You said `digits`. `Shift` seems like an easier key to work with. Easy enough to turn `#` into `3`. – Sam Axe Mar 11 '15 at 21:29
  • 1
    I see what you're trying to do. How about using a timer set to something like 150ms? So that focus would only shift after the delay has elapsed and no additional character was entered during that time frame. If new character is entered reset the timer so it counts from 0 to 150ms again. – bokibeg Mar 11 '15 at 22:24

1 Answers1

1

Like the commenters said, there's probably a better way of doing this, in a UI sense. That you would press CTRL to prevent switching textboxes is weird behavior. Having said that, using the following in KeyDown should get you what you were trying to do.

if (!(e.Control && char.IsLetterOrDigit((char) e.KeyCode)))
{
    return;
}

txtBox.Text += (new KeysConverter()).ConvertToString(e.KeyCode);
e.Handled = true;
Misha Akopov
  • 12,241
  • 27
  • 68
  • 82
DrewJordan
  • 5,266
  • 1
  • 25
  • 39