C # handling keystrokes

private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
    if (e.KeyChar < '0' || e.KeyChar > '9')
        if (e.KeyChar != '\b')
            e.Handled = true;

}

I do not understand how this code does not allow anything but backspace and numbers.

  • the first if statement says that if it is not 0 - 9, then do nothing?
  • the second one says that if he does not backspace does nothing?
  • making e.Handled=True?
+3
source share
3 answers

The first operator ifbasically says that if it is a number, let it act as usual - otherwise go to the second operator if.

if , backspace, - .

e.Handled = true; , , . , .

:

bool isDigit = e.KeyChar >= '0' && e.KeyChar <= '9';
bool isBackspace = e.KeyChar == '\b';

// If we get anything other than a digit or backspace, tell the rest of
// the event processing logic to ignore this event
if (!isDigit && !isBackspace)
{
    e.Handled = true;
}
+10

, e.Handled=true,

+1

1 2: . , " 0-9, , backspace. backspace, e.Handled ".

3: e.Handled true, , , , . e.Handled : " , ".

+1

Source: https://habr.com/ru/post/1769601/


All Articles