private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar < '0' || e.KeyChar > '9')
if (e.KeyChar != '\b')
e.Handled = true;
}
i am not understanding how this code is not allowing anything except backspace and numbers.
- the first if statement is saying if it is not 0 – 9 then dont do anything?
- the second one is saying if it’s not backspace dont do anything?
- what does
e.Handled=Truedo?
The first
ifstatement is basically saying if it is a digit, allow it to proceed as normal – otherwise go into the secondifstatement.The second
ifstatement is saying that if it’s also not backspace, allow it to proceed as normal – otherwise go onto the assignment statement.e.Handled = true;indicates that the event handler has already processed the event and dealt with it, so it doesn’t need to be processed any further. In other words, please don’t take any further action.Here’s an alternative way of writing the same body: