One routine for all textboxes on form

Oct 18 2017 3:11 AM

Dear Sir,

I am using following codes on EVERY textbox.

  1. private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
  2. {
  3. char ch = e.KeyChar;
  4. decimal x;
  5. if (ch == (char)Keys.Back)
  6. {
  7. e.Handled = false;
  8. }
  9. else if (!char.IsDigit(ch) && ch != '.' || !Decimal.TryParse(textBox1.Text + ch, out x))
  10. {
  11. e.Handled = true;
  12. }
  13. }
  14. private void textBox1_Leave(object sender, EventArgs e)
  15. {
  16. if (textBox1.Text == "")
  17. {
  18. MessageBox.Show("Please enter Num 1");
  19. return;
  20. }
  21. else
  22. {
  23. textBox1.Text = Convert.ToDecimal(textBox1.Text).ToString("#,##");
  24. }
  25. }

In this way I have to waste much time and form space.

Is there any other way to use a routine on InitilizeCompent to handle all textboxes?

Please help


Answers (2)