Dano9700
Dano9700

Reputation: 13

How can I change the properties of controls in all tabs in Windows Forms Application?

I have been searching for this answer for a while with no luck. It seems like a fairly simple question but I can't come up with anything, so here goes:

I want to set all the controls in all of my tabs to ReadOnly = true or Enabled = false (or the reverse).

I'm using this code:

public void readOnly(bool read)
    {
            if (read == true)
            {
                foreach (var c in mainTab.Controls)
                {
                    if (c is TextBox)
                    {
                        ((TextBox)c).ReadOnly = true;
                    }
                    if (c is CheckBox)
                    {
                        ((CheckBox)c).Enabled = false;
                    }
         Etc. ......

How can I apply changes to all tabs, not just mainTab, without repeating the code? I'm new to programming so I apologize if I'm missing something obvious...

If this question has already been answered could you kindly point me to the page?

Upvotes: 0

Views: 460

Answers (3)

DanielVorph
DanielVorph

Reputation: 131

You need a recursive function:

public static void EnabledDisabeldControls(System.Windows.Forms.Control.ControlCollection paramControls, bool enabled)
    {

        foreach (System.Windows.Forms.Control c in paramControls)
        {
            if (c is TextBox)
                {
                    ((TextBox)c).ReadOnly = !enabled;
                }
                if (c is CheckBox)
                {
                    ((CheckBox)c).Enabled = enabled;
                }
            EnabledDisabeldControls(c.Controls, enabled);
        }
    }

Upvotes: 0

adjan
adjan

Reputation: 13652

To access all tabs, you can use the TabPages property of the TabControl. Further, you can significantly simplify your code:

public void SetReadOnly(bool readOnly)
{
    foreach (TabPage tab in tabControl.TabPages)
    {
        foreach (Control c in tab.Controls)
        {
             if (c is TextBox)
             {
                 ((TextBox)c).ReadOnly = readOnly;
             }
             else
             {
                 // All controls support this property
                 c.Enabled = !readOnly;
             }
        }
    }
}

Upvotes: 1

Rufus L
Rufus L

Reputation: 37020

You could create a method that will loop through the controls of each tab page in your tab control and set the Enabled property of the controls, and have a Boolean argument that says what the value should be:

private void SetTabControlEnabled(bool enabled)
{
    foreach (TabPage tabPage in tabControl1.TabPages)
    {
        foreach (Control control in tabPage.Controls)
        {
            control.Enabled = enabled;
        }
    }
}

Then, as an example, you could call this method and pass true to enable the controls or false to disable them:

private void btnEnable_Click(object sender, EventArgs e)
{
    SetTabControlEnabled(true);
}

private void btnDisable_Click(object sender, EventArgs e)
{
    SetTabControlEnabled(false);
}

Upvotes: 0

Related Questions