Kevin Miller Kevin Miller - 3 months ago 20
C# Question

C# Errorprovider.ContainerControl.Controls contains just TabControl

I have a Form that contains a TabControl and an ErrorProvider. One of the tabs has several textboxes and a button. The textboxes use the Validating event to SetError() if contents are not valid. When the button is pressed, this runs:

bool ok = true;
foreach (Control c in errorProviderDv.ContainerControl.Controls)
{
MessageBox.Show(c.Name);
if (errorProviderDv.GetError(c) != "")
{
ok = false;
}
}


The TabControl is the only control in errorProviderDv.ContainerControl.Controls, even though several errors are set and are displaying in the form.

Am I doing something wrong? Does the ErrorProvider need to be a child of the tab instead of the form?

Answer

The TabControl itself is a container which contains TabPages. Those TabPages are containers which contain your textboxes.

The following code will get you want you want but you may want to clean it up to make a recursive call passing in a container so it will work for almost any type.

    private void button1_Click(object sender, EventArgs e)
    {
    bool ok = true;
    foreach (Control c in errorProviderDv.ContainerControl.Controls)
    {
        if (c is TabControl)
        {
            foreach (Control t in (c as TabControl).SelectedTab.Controls)
            {
                MessageBox.Show(t.Name);
                if (errorProviderDv.GetError(t) != "")
                {
                    ok = false;
                }
            }
        }
    }
}
Comments