Link to home
Start Free TrialLog in
Avatar of jrram
jrramFlag for United States of America

asked on

ErrorProvider component only sets for last control...why?

Here is my class:

public class superComboBox : ComboBox
{
      private bool is_required = false;

      private ErrorProvider ctrlErr = new ErrorProvider();
      private string err_msg = "";
            
      public bool Required
      {
            // Retrieves the value of the private variable is_required
            get
            {
                  return is_required;
            }
            // Stores the selected value in the private variable is_required
            set
            {
                  is_required = value;
            }
      }
            
      public string ErrorMessage
      {
            // Stores the selected value in the private variable is_required
            set
            {
                  err_msg = value;
                  ctrlErr.SetError(this, err_msg);
            }
      }
}

And here is how I'm using it on my page:

private bool validateComboBox(superComboBox ctrlToTest)
{
      if (ctrlToTest.Required == true)
      {
            if (ctrlToTest.SelectedIndex == 0)
            {
                  ctrlToTest.ErrorMessage = "The\"Select...\" option is not a valid choice.";            
                  return false;
            }
      }

      return true;
}

Let's say I have 3 drop-down controls on my form.  If all 3 controls fail validation, I'd expect to see the red splat besides each control.  But, it only appears on the last control and I can't figure out why.  I know that you can't dynamically set the name for a error provider control so that's why I thought putting it in a class would help.  To no avail though, so help me out people :-)
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

The ErrorProvider component has a one-to-one relationship between a control and an error condition.  If you have 3 controls, then you need 3 error providers.

Bob
Avatar of jrram

ASKER

That's strange, cause it works if I use it on multiple panel controls.
What do you mean by "it works if I use it on multiple panel controls"?

Does that mean that you have one error provider, and multiple controls with red error conditions?

Bob
Avatar of jrram

ASKER

I got it to work.  All I had to do is declare my ErrorProvider declaration at the beginning of the program and it worked.  I think the problem before was I kept re-defining the component.

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Xml;

namespace ResizeMe
{
      /// <summary>
      /// Summary description for part1.
      /// </summary>
      public class actSurvey : System.Windows.Forms.Form
      {
            private System.Windows.Forms.Button cancel;
            private System.Windows.Forms.TabPage tabPage1;
            private System.Windows.Forms.TabPage tabPage2;
            private System.Windows.Forms.Panel panel2;
            private System.Windows.Forms.Panel panel1;
            private System.Windows.Forms.Panel processPanel;
            private System.Windows.Forms.TabControl surveyBook;
            private System.Windows.Forms.Button saveSurvey;
            private System.ComponentModel.IContainer components;

            private ErrorProvider formErrorHandler = new ErrorProvider();

            private bool validateTabs()
            {
                  int failedControls = 0;

                  foreach(TabPage tp in surveyBook.TabPages)
                  {
                        foreach(Control ctrl in tp.Controls)
                        {                                    
                              if (ctrl.GetType() == typeof(superTextBox))
                              {
                                    if (!validateTextBox((superTextBox) ctrl)) failedControls+=1;
                              }
                              else if (ctrl.GetType() == typeof(superComboBox))
                              {
                                    if (!validateComboBox((superComboBox) ctrl)) failedControls+=1;
                              }
                              else if (ctrl.GetType() == typeof(radioPanel))
                              {
                                    if (!validateRadioPanel((radioPanel) ctrl)) failedControls+=1;
                              }
                        }
                  }

                  return failedControls == 0 ? true :  false;
            }

            private bool validateComboBox(superComboBox ctrlToTest)
            {                        
                  formErrorHandler.SetIconAlignment(ctrlToTest, ErrorIconAlignment.TopRight);
                  formErrorHandler.SetError(ctrlToTest, String.Empty);

                  if (ctrlToTest.Required == true)
                  {
                        if (ctrlToTest.SelectedIndex == 0)
                        {                        
                              formErrorHandler.SetError(ctrlToTest, "The\"Select...\" option is not a valid choice.  Please choose another option!"
                                                + "\n\nMore Info: " + ctrlToTest.MoreInfo);
                              
                              return false;
                        }
                  }

                  return true;
            }

      }
}
ASKER CERTIFIED SOLUTION
Avatar of ee_ai_construct
ee_ai_construct
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial