I have WinForms app and multiple forms and want to use ErrorProvider component on each of them EDIT: to check if user input is OK (for example if entered number is in the range etc…) It seems to me useless to drop this component onto each and every form. What if I make one global object (or how to call it) and use it on every form?
My idea:
namespace MyApplication {
static class Program {
public static ErrorProvider EP = new ErrorProvider();
...
And then in that individual form to handle Validating and Validated events:
private void txtBox1_Validating(object sender, System.ComponentModel.CancelEventArgs e) {
if (txtBox1.Text != "correct text") {
e.Cancel = true;
Program.EP.SetError(txtBox1, "You have error in your input");
...
Is this correct approach or should I do it somehow else?
And if I need more global objects, maybe I should put them all together to some separate static class and in the Program create just this one (?)
Thanks.
I think you can create a new class that inherits textbox class and add your validations there, then create a new control which is your class and use it where ever you need, in this case you will edit/modify your validations in one place later on.
I f you have very different validations create 2 classes for example: NumericTextBox and StringTextBox