Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# ErrorProvider Want to know if any are Active

I want to know if any ErrorProvider are active in my form. being able to find this out might help reduce my code..

I did find this thing here Counting ErrorProvider

but incase someone knows a better way... so here goes.

Ok so basically I have a WinForm which has many TextBoxes Now when user enters values I use Validating to perform validation and if it does not match Regex I set the ErrorProvider ON for that Control.. similarly if the user changes the value to a acceptable one I switch ErrorProvider OFF for that Control..

but when SAVE is clicked i have to do another check anyways incase the user did not listen to me and change the thing like he was supposed to and still clicked SAVE.. I dont want the thing crashing..

soo mm is there like a thing where I could say if ErrorProviders is not active then proceed with save else message box saying change it.

[ANOTHER QUESTION]

Umm When Validating it only Validates when the Control loses Focus... I kinda of want it to do validation when user stops typing.. I hope you get what I mean

Like Email Address(textbox) when user is typing his/her name in I [DON'T] want it to do validation yet, but when user has finished entering is waiting for ErrorProvider to disappear(But it doesn't coz it only does that when control loses focus) 2 odd seconds after typing can i make the validation take place?

like image 991
RcK Avatar asked Sep 07 '12 17:09

RcK


2 Answers

Unfortunately, the ErrorProvider control doesn't provide such functionality. You'd best go with the custom error provider classes from the link you posted.

Otherwise, you could create a method that you would call instead of SetError

int errorCount;
void SetError(Control c, string message)
{
    if (message == "")
        errorCount--;
    else
        errorCount++;
    errorProvider.SetError(c, message);
}

Or you could make an extension method for the ErrorProvider class that would set the error and increment a counter or something along those lines.

And last but not least, you could iterate through all the controls. Slow, but it works:

bool IsValid()
{
    foreach (Control c in errorProvider1.ContainerControl.Controls)
        if (errorProvider1.GetError(c) != "")
            return false;
    return true;
}

Edit

I've written a quick extension class for the error provider:

public static class ErrorProviderExtensions
{
    private static int count;

    public static void SetErrorWithCount(this ErrorProvider ep, Control c, string message)
    {
        if (message == "")
        {
            if (ep.GetError(c) != "")
                count--;
        }
        else
            count++;

        ep.SetError(c, message);
    }

    public static bool HasErrors(this ErrorProvider ep)
    {
        return count != 0;
    }

    public static int GetErrorCount(this ErrorProvider ep)
    {
        return count;
    }
}

I haven't tested it extensively, so you might want to do a bit more validation before calling SetError on your ErrorProvider.

like image 69
Netfangled Avatar answered Sep 28 '22 07:09

Netfangled


I know this is a bit older question and the extension is working except if someone try to SetErrorWithCount twice for the same object, the count is counted twice. so, here I come with the update extension base on Netfangled extension

public static class ErrorProviderExtensions
{
   private static int count;

   public static void SetErrorWithCount(this ErrorProvider ep, Control c, string message)
   {
       if (message == "")
       {   
          if (ep.GetError(c) != "")
             count--;
       }
       else
          if (ep.GetError(c) == "")
             count++;

       ep.SetError(c, message);
   }

   public static bool HasErrors(this ErrorProvider ep)
   {
       return count != 0;
   }

   public static int GetErrorCount(this ErrorProvider ep)
   {
       return count;
   }
}
like image 33
Hendra Neo Avatar answered Sep 28 '22 08:09

Hendra Neo