Iterate through all error provider messages, and display on clickEvent ?c# winform

370 Views Asked by At

I am creating a winform projects which utilities multiple error providers, I have unique errorproviders for each textBox. I'm trying to iterate through all of them and display all that are present in a messagebox.

So, I know I have to loop through my controls which I am doing correctly all my issue is that I cannot print out the specific textboxes which have errors. This is what I have, however the message Box statement is incorrect.

var errorProviders = new List<ErrorProvider>() {epPrefConTime, epPrefConNumber, epTitle, epEmail, epAlternative, epMobile, epTown, epLandline, epHouseName, epForeName, epSurname, epPostcode, epCountry, epHouseName, epLocality, epCounty };

    foreach (Control c in panel1.Controls)
    {
        if (c is SpellBox)
        {
            if (errorProviders.Any(e => e.GetError(c).Length > 0))
            {

                MessageBox.Show(errorProviders.Any(e => e.GetError(c).ToString()));
                return true;
             }

        }
        else if (c is TextBox)
        {
            if (errorProviders.Any(e => e.GetError(c).Length > 0))
            {
                epCountry.SetError(btn_SaveDetails, "Errors present in form, please review!!!");
                return false;
            }
        }
        MessageBox.Show(e.GetError(c));
        return true;
    }

I'm Mapping my errorProviders in my my Validating events as follows for all 16 textBoxes:

private void txt_Forname_Validating(object sender, CancelEventArgs e)
        {
            if (util.useUnkownEntity(txt_Forname.Text))
            {
                UnkownEntity = true;
                epForeName.SetError(txt_Forname, "Please use unknow entity!!");
                return;
            }

            else if (string.IsNullOrWhiteSpace(txt_Forname.Text))
            {
                _IsValid = false;
                return;
            }

            else if (util.IsAllLetters(txt_Forname.Text))
            {
                epForeName.Clear();
                txt_Forname.Text = util.ToTitle(txt_Forname.Text);
                _IsValid = true;
                UseEntity = false;
                return;
            }

            else
            {
                epForeName.SetError(txt_Forname, "InValid Forename, please  reType!!"); _IsValid = false;
                return;
            }

        }

If someone could help me ascertain how to message out all of my errors provider messages or even Which text boxes the errors are in

0

There are 0 best solutions below