• 0

[C#] Validation class for events?


Question

Hi,

I develop an application that have 20 windows forms with many TextBoxes.

All TextBoxes have the same validation...

I write in all classes the same validation like that:

 
  //if the user press a key down at txtDisplacement we check which one was pressed down
  #region KeyDownInTxtDisplacement code
  private void KeyDownInTxtDisplacement(object sender, System.Windows.Forms.KeyEventArgs e)
  {
 	 CheckWhichKeyDown(e);
  }
  #endregion



  //if the user press a key down at txtModel we check which one was pressed down
  #region KeyDownInTxtModel code
  private void KeyDownInTxtModel(object sender, System.Windows.Forms.KeyEventArgs e)
  {
 	 CheckWhichKeyDown(e);
  }
  #endregion



  //we check which key is pressed
  //if key Enter or Return or Tab is pressed
  //we go throw DoWhenOneTextBoxIsMarkedRed()
  #region CheckWhichKeyDown code
  private void CheckWhichKeyDown(System.Windows.Forms.KeyEventArgs e)
  {
 	 if(e.KeyCode==Keys.Enter || e.KeyCode==Keys.Return || e.KeyCode==Keys.Tab)
 	 {
    ValidateThis();
 	 }  
  }
  #endregion



  //when special key is press we enable the button
  #region ValidateThis code
  private void ValidateThis()
  {
 	 cmdAddToDataBase.Enabled = true;
  }
  #endregion

CheckWhichKeyDown and ValidateThis should be in one validation class.

How I can write a validation class that validate ALL TextBoxes from all classes.

Any ideas?

Or do you know some sample validation classes available on www?

gicio

Link to comment
https://www.neowin.net/forum/topic/57015-c-validation-class-for-events/
Share on other sites

1 answer to this question

Recommended Posts

  • 0

Something like that....

public class TextValidation

{

public static void Validate(System.Windows.Forms form)

{

foreach(Control ct in form)

{

if(ct is System.Windows.Forms.TextBox)

{

ValidateThis();

}

}

}

}

Add to this class CheckWhichKeyDown and ValidateThis. (make them static)

....and you can call Validate for all your classes.

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.