How to use error provider at run time along with associating any control to validate

Posted by Shantanu Gupta on Stack Overflow See other posts from Stack Overflow or by Shantanu Gupta
Published on 2010-04-20T05:14:02Z Indexed on 2010/04/20 5:23 UTC
Read the original article Hit count: 259

Filed under:
|
|
|
|

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

© Stack Overflow or respective owner

Related posts about winforms

Related posts about c#