Search code examples
c#visual-studiowinformskeypress

KeyPress event using textbox C# winform


I have lot of textbox in my winform. maybe 20 to 30 textbox. and I need all texbox have numerical only when typing.

I use keypress event per textbox to handle the numerical only(code below)

 if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) && (e.KeyChar != '.'))
            {
                e.Handled = true;
            }
            // only allow one decimal point
            if ((e.KeyChar == '.') && ((sender as TextBox).Text.IndexOf('.') > -1))
            {
                e.Handled = true;
            }

is there possible way to inherit all textboxes in just 1 keypress event?


Solution

  • Implement the keypress event for only one textbox.

    Then select another text box and go to keypress event and select the method you already implemented.

    enter image description here