views:

283

answers:

1

I am trying to create a Validation in a reusable fashion.

Purpose: Make the validation control reusable.

Error Provider should associate with control passed dynamically and can be set or cleared at run time.

When user press OnClick event then all the controls gets validated with their own Error Providers.

public bool IsFieldEmpty(ref TextBox txtControl, Boolean SetErrorProvider,string msgToShowOnError)
{
    ErrorProvider EP = new ErrorProvider();
    if (txtControl.Text == string.Empty)
    {
        if(SetErrorProvider==true)
            EP.SetError(txtControl, msgToShowOnError);
        return true;
    }
    else
    {
        if(SetErrorProvider==true)
            EP.Clear();
        return false;
    }
}

Issue:

Every time the function is called new errorprovider object gets created which i dont want. Every control should not have more than 1 error provider and i should be able to search it just like as done in asp.net to search for some control on a Page.

How can I do this

A: 

In most cases you really only need one instance of ErrorProvider on a form.

E.g.

ErrorProvider errorProvider1 = new ErrorProvider();

or just drag one from the toolbox onto the form.

When calling an ErrorProvider, you supply the control and the message,,

errorProvider1.SetError (dateTimePicker1, "HEY BAD DATE");

To clear the error...

errorProvider1.SetError (dateTimePicker1, "");

So, one ErrorProvider instance is all you really need in most situations.

Sky Sanders
@Sky: Thx for help. I am able to do with single EP only. But I was thinking to make it work for multiple contols. If user submits form. Then all EP becomes visible at a time so that user need not press submit btn every time to pass correct value
Shantanu Gupta
@Shantanu - just change your code, move the `ErrorProvider EP = new ErrorProvider();` outside the function and into the form so you have just one EP. EP is not control specific, that is why you pass in the control when setting an error. Is there something that is missing from your question that would indicate that this is not a viable option?
Sky Sanders
@Sky: I only want to create instance of EP at runtime when i want to validate. i.e. SetErrorProvider filed passed in function will tell me whether to create error provider for the current control. Now here at this point I want to check whether this current control already contain any error provider or not. If no create it else find that error provider for that control and do your work. I hope my question would be clear to you. Please let me know any suggestion.
Shantanu Gupta
@Shantanu - I can see no compelling reason to add complexity to a very simple scenario. Controls do NOT *contain* ErrorProviders. An Error provider is an autonomous component that does not know, nor needs to know anything about the control that is being validated. There is typically no need to have more than one EP. I think you may not have a clear understanding of the function of the EP. Please see http://msdn.microsoft.com/en-us/library/system.windows.forms.errorprovider.aspx and walk through the sample. I think things will become clear.
Sky Sanders