Storing error providers in dynamic variables

147 Views Asked by At

OverView

I am working on this project which has 12 error providers all of which have their own unique name, and purpose. I know I can use one however I need to ensure that they all have their own error message for the user. The project is winform and i want all of the validation to be evaluated on my save button, which subscribes to a click event.

My issue

I need to be able to asses whether any error providers are active before continuing with the save function. I have this following code which works however it is rather cumbersome and long-winded. I also need to display the error messages to the user, so they can indicate which field s are invalid.

Validation Method

bool IsValidIn()
            {
                foreach (Control c in panel1.Controls)
                {
                    if (c is SpellBox)
                    {
                        //SpellBox txt = (SpellBox)c;
                        if (epForeName.GetError(c).Length > 0 || epSurname.GetError(c).Length > 0 
                                                              || epPostcode.GetError(c).Length > 0
                                                              || epCountry.GetError(c).Length > 0
                                                              || epCounty.GetError(c).Length > 0
                                                              || epHouseName.GetError(c).Length > 0
                                                              || epLocality.GetError(c).Length > 0
                                                              || epStreetName.GetError(c).Length > 0
                                                              || epMobile.GetError(c).Length > 0
                                                              || epLandline.GetError(c).Length > 0
                                                              || epAlternative.GetError(c).Length > 0
                                                              || epEmail.GetError(c).Length > 0)
                        { return false; }
                    }
                }
                return true;}

Save Click event

 public void btn_SaveDetails_Click(object sender, EventArgs e)
        {
            try
            {

                //txt_ForeName_Validated(this, e);


                if (IsValidIn())
                {

                    _IsValid = true;
                    BtnPressed = "Ok";
                    HouseName = txt_HouseName.Text;
                    HouseNumber = Convert.ToString(nud_HouseNumber.Value);
                    StreetName = txt_StreetName.Text;
                    Locality = txt_Locality.Text;
                    Town = txt_Town.Text;
                    County = txt_County.Text;
                    Country = txt_Country.Text;
                    PostCode = txt_Postcode.Text;
                    Email = txt_Email.Text;
                    Title = cmb_Title.Text;
                    BirthDate = Convert.ToString(dateTimePicker1.Text);
                    ForeName = txt_ForeName.Text;
                    SurName = txt_SurName.Text;
                    PrefContNum = cmb_PrefConNumber.Text;
                    PrefContTime = cmb_PrefConTime.Text;
                    Mobile = txt_Mobile.Text;
                    Landline = txt_LndLine.Text;
                    Alternative = txt_Alt.Text;

                    this.Close();

                }
                else
                {
                    MessageBox.Show("Errors present in form, please review!!!"); //MessageBoxButtons.YesNo) == DialogResult.Yes);
                }
            }

Possible solution.

I was thinking of possibly storing the variables into a collection of some-sort, more than likely a dictionary, by storing my variables into a dynamic variable, then iterate through the Error-providers themselves however I am really unsure of the possibility of whether it will even be possible. If there is a simple way of refactoring this function I would be very appreciated if someone could help.

2

There are 2 best solutions below

2
Kemal Güler On

I think this will give what you want

bool IsValidIn()
            {
                foreach (Control c in panel1.Controls)
                {
                    if (c is SpellBox)
                    {
                        //SpellBox txt = (SpellBox)c;
                        string errorStr = string.Empty;
                        if (epForeName.GetError(c).Length > 0)
                            errorStr = "epForeName";
                        else if(epSurname.GetError(c).Length > 0)
                            errorStr = "epSurname";
                        .
                        .
                        .
                        else if(epEmail.GetError(c).Length > 0)
                            errorStr = "epEmail";


                        if(errorStr != String.Empty) 
                            return false; 

                    }
                }
                return true;}

// just return the errorStr for getting error.

6
Jon B On

There's no need to use dynamics here. You can simply store your ErrorProvider objects in a List<ErrorProvider>. Use errorProviders.Any(e => e.GetError(c).Length > 0) to determine if any error exists.