0

I seem to have a very simple problem, but don't understand why it seems to not work atm.

I have to the keypress event attached this function.

    void length_textbox_numeric_check(object sender, KeyPressEventArgs e)
    {
        if ((char.IsNumber(e.KeyChar)) && (char.IsControl(e.KeyChar)) )
        {
            e.Handled = true;
        }
        else
        {
            e.Handled = false;
        }
    }

it accept both character, number and spaces and so on?... but according the code should it only accept numbers and backspace? so why is it also accepting letters?

Lke
  • 11
  • 3
  • 2
    setting KeyPressEventArgs.Handled as true is saying that the character is handled and does not need to be processed anymore (i.e. its not going to get put into the textbox) – Blake Thingstad Feb 15 '18 at 15:19
  • 2
    Your expression will always be false - the character cannot be a number AND a control character at the same time. So it will always be setting e.Handled to false - which allows all cahracters to be accepte. – PaulF Feb 15 '18 at 15:20
  • ok... I am not sure I understand the logic behind it... I guess that's the way the cookie crumbles.. – Lke Feb 15 '18 at 15:23
  • I think you understand the Handled now that I see other answers about the if condition, so I deleted my answer about it, let me know if this is a source of confusion. – Blake Thingstad Feb 15 '18 at 15:26
  • I guess what confuses me is the wording.. I guess the semantics could have been a bit clearer.. but hey.. its ok.. – Lke Feb 15 '18 at 15:38
  • You are using it correctly at least. If you do not want the character to be entered into the text box then set e.Handled = true, by default it is set to false (so you actually don't need the else). – Blake Thingstad Feb 15 '18 at 15:42

2 Answers2

1

Based on @BlakeThingstad comment. This fixed it.

   void length_textbox_numeric_check(object sender, KeyPressEventArgs e)
    {
        if (!(char.IsNumber(e.KeyChar)) && !(char.IsControl(e.KeyChar)) )
        {
            e.Handled = true;    // Handled states whether it should handled
                                 // normally (true) or differently (false) 
        }
        else
        {
            e.Handled = false;
        }
    }
Lke
  • 11
  • 3
  • 1
    To be accurate, setting the Handled property to true indicates that the key press has been handled by the event handler & so should not be processed further. Setting it to false indicates it has not been dealt with in the event, so should be passed on to the default event handler. – PaulF Feb 15 '18 at 15:39
  • The [documentation](https://msdn.microsoft.com/en-us/library/system.windows.forms.keypresseventargs.handled(v=vs.110).aspx) states this well, "If the event is not handled, it will be sent to the operating system for default processing. Set Handled to true to cancel the KeyPress event." – Blake Thingstad Feb 15 '18 at 15:43
0

You want to suppress the KeyPress. Don't know which event you listened. I would go for KeyDown:

    private void textBox1_KeyDown(object sender, KeyEventArgs e)
    {
        if (!char.IsNumber((char)e.KeyValue) && !char.IsControl((char)e.KeyValue))
            e.SuppressKeyPress = true;
    }
kara
  • 3,205
  • 4
  • 20
  • 34
  • I went for KeyPress... Why do you suggest KeyDown? This is a just a dummy project - but what cases would it make sense? – Lke Feb 15 '18 at 15:28
  • Normally you want to know when the user starts to do something in your textbox. Not if he started pressing it a minute ago and changed focus or something. https://stackoverflow.com/questions/5871383/difference-between-the-keydown-event-keypress-event-and-keyup-event-in-visual-s – kara Feb 15 '18 at 15:31
  • @Lke: check out the documentation for both. It may not be an issue for your project but you will see the KeyPress event does not allow you see [some characters](https://msdn.microsoft.com/en-us/library/system.windows.forms.keypresseventargs.keychar(v=vs.110).aspx). other events, such as KeyDown, let you see all characters & modifiers (CTRL/ALT etc). – PaulF Feb 15 '18 at 15:35