6

I've a textbox with an event that should do things when some text is entered. It's easy to check if it is alphanumeric as stated here Can I determine if a KeyEventArg is an letter or number? :

if ( ( ( e.KeyCode >= Keys.A && e.KeyCode <= Keys.Z ) ||
( e.KeyCode >= Keys.D0 && e.KeyCode <= Keys.D9 ) ||
( e.KeyCode >= Keys.NumPad0 && e.KeyCode <= Keys.NumPad9 ) )

The problem with this approach is that I should also check manually for -?!¿[]() with Key.OemMinus, Key.OemQuestion, etc.

Is there some way to check if it's a text keystroke or I should check manually (which is not very elegant in my opinion)?

Cœur
  • 37,241
  • 25
  • 195
  • 267
David Fornas
  • 362
  • 1
  • 5
  • 18
  • 1
    Do you really want to exclude all chars that don't have a key? For instance my keyboard doesen't have a `Є` key but arguably it is still text. – Jodrell Aug 09 '12 at 10:28
  • That's a good question because I should be able to use, for example, ñ to trigger this event. I supouse I should check if this character is printable or something similar. I want to exclude Ins, Arrows, F's, Enter, Tab, etc. – David Fornas Aug 09 '12 at 10:32
  • 1
    Check the text against a Regex can be a solution? – michele Aug 09 '12 at 10:35
  • The problem is that I don't know hot to convert, for example, from OemQuestion to ?. Solving that may help to the problem. – David Fornas Aug 09 '12 at 10:36
  • 1
    The regex `\p{C}` matches invisible control characters and unused code points. Whether or not those charachters map to a key is irrelavent. – Jodrell Aug 09 '12 at 10:44
  • http://gskinner.com/RegExr/ will help to build up your regex. Maybe something like [€\[\]\-\?\!\¿\(\)] to get what you want. – michele Aug 09 '12 at 10:49
  • Yes, but first I can't use that expression to differentiate between OemQuestion (?) and Enter beacuse I don't have an easy way to convert to ? and check RE – David Fornas Aug 09 '12 at 10:49
  • 1
    Just for clarification when I press ? I get that e.Key equals Key.OemQuestion, from which I cannot get ?. – David Fornas Aug 09 '12 at 10:50

2 Answers2

3

As no other option is suggested I used the following code to allow nearly all text keystrokes. Unfortunatelly, this is keyboard dependant so it's not very elegant. Hopefully is not a critical aspect in the application, it's only a matter of usability.

bool isText = (e.Key >= Key.A && e.Key <= Key.Z) || (e.Key >= Key.D0 && e.Key <= Key.D9) || (e.Key >= Key.NumPad0 && e.Key <= Key.NumPad9)
            || e.Key == Key.OemQuestion || e.Key == Key.OemQuotes || e.Key == Key.OemPlus || e.Key == Key.OemOpenBrackets || e.Key == Key.OemCloseBrackets || e.Key == Key.OemMinus
             || e.Key == Key.DeadCharProcessed || e.Key == Key.Oem1 || e.Key == Key.Oem7 || e.Key == Key.OemPeriod || e.Key == Key.OemComma || e.Key == Key.OemMinus
              || e.Key == Key.Add || e.Key == Key.Divide || e.Key == Key.Multiply || e.Key == Key.Subtract || e.Key == Key.Oem102 || e.Key == Key.Decimal;
David Fornas
  • 362
  • 1
  • 5
  • 18
0

this code allow just numbers and '.':

    private void txtJustNumber_KeyPress(object sender, KeyPressEventArgs e)
    {
        if (!char.IsDigit((char)(e.KeyChar)) &&
            e.KeyChar != ((char)(Keys.Enter)) &&
            e.KeyChar != (char)(Keys.Delete) &&
            e.KeyChar != (char)(Keys.Back))             
        {
            e.Handled = true;
        }
    }
mjyazdani
  • 2,110
  • 6
  • 33
  • 64