I've just begun to learn C# and am trying to create a text box that will accept only integers and backspace key. I've created the code for doing that within the 'KeyPress' event for the text box:
private void textBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
if (!Char.IsDigit(e.KeyChar) && e.KeyChar != (char)8) { e.Handled = true; }
if (!Char.IsDigit(e.KeyChar) && e.KeyChar != (char)8)
e.Handled = true;
}
I realize that I can use a MaskedTextBox, but my real interest is in trying to understand how to use the above example to better understand OOP. I would like to create a generic method that can be called upon from any number of text boxes and I'm not sure how to do that for a KeyPress event. If someone can provide an example with some explanatory notes, I would greatly appreciate it.
Thank you,
imadman