Link to home
Start Free TrialLog in
Avatar of Gani tpt
Gani tpt

asked on

How to disable all controls except one or two controls in tab page Windows C#

Hi,

I am using multiple tab pages and each tab will have many controls.

Except few contols (textbox1 and radiobutton1), remaining controls should get disabled in tab page1.

How to do this in windows c#..?
Avatar of Éric Moreau
Éric Moreau
Flag of Canada image

You can loop through the controls of the tabpage and disable the controls after checking their name property. Or even better, use the Tag property of the controls to specify if a control has to be disable or not (and use the Tag property while looping through the controls).
You can loop through the controls in the active tab

 foreach(Control c in currentTab.Controls)
{
    if(c is CheckBox)
        //disable control
    if(c is ComboBox)
        //disable control
}

Open in new window


Add all your controls here except Textbox and Radio box
Avatar of Gani tpt
Gani tpt

ASKER

ca i have sample code's...
I have already posted use as above and let us know
foreach(Control c in currentTab.Controls)
{
    if(c is CheckBox)
        //disable control
    if(c is ComboBox)
        //disable control
}

Open in new window

button1 disabled almost everything and button2 re-enables everything
        private void button1_Click(object sender, EventArgs e)
        {
            foreach (Control c in tabPage1.Controls)
            {
                if (!(c.Name == "textBox1" || c.Name == "radioButton1"))
                    c.Enabled = false;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            foreach (Control c in tabPage1.Controls)
            {
                c.Enabled = true;
            }
        }

Open in new window

i used below code..but, not working..

public frmMain()
{
 int index = 0; // which tab you want 0-based index
 (tablcontrol1.TabPages[index] as Control).Enabled = false;
  foreach (Control c in tskSelectionTab.TabPages[index].Controls)
  {
                    if (c is TextBox)
                    {
                        if (c.Text == "txtEmpNo")
                        {
                            c.Enabled = true;
                        }
                    }
  }
}
Why you are comparing with Text? do you want to disable them by Id?
Hi Ganesh,

Another trick I used a lot in past was to use Groupbox. It provides visual as well as logical grouping of controls. So I put all the controls in a group(or two or more) and then whenever I need to disable all those controls I just disable the Groupbox and by design all the child controls will be disabled. So in your case, on a tab page, add a Groupbox and then put those controls which needs to be enabled/disabled programmatically on to that Groupbox and you are good to go.

Regards,
Chinmay.
not Text, Name
Yes. absolutely i have been using group box.

my Textbox1 placed inside the group box only.

i tried below updated code..but, still not enable in Textbox1.

my groupbox name : "groupBox1"

 int index = 0; // 1st tab
 (tablcontrol1.TabPages[index] as Control).Enabled = false; //Disable all the controls in the page
 foreach (Control c in Emptab.Controls) // Find all the group and controls
 {
    if (c.Name == "groupBox9") // Textbox1 is placed insdie the groupbox
     c.Enabled = true; //trying to eanble groupbox with textbox
 }

not able to enable.....?
if the container (tabpage or groupbox) is disabled, a control inside that container cannot be enabled. the container also has to be enabled.
Hi Ganesh,

For Groupbox, you don't have to do anything at all. Assuming all the controls that you want to disable are inside the Groupbox then you just need this code.

groupBox1.Enabled = false;

Open in new window


And in your case, wouldn't it be like this?
[/code]
if (c.Name == "groupBox1") // Textbox1 is placed insdie the groupbox
     c.Enabled = true; //trying to eanble groupbox with textbox
[/code]

Regards,
Chinmay.
Thanks Chinmay.

i am using many group box in the page.

Fro textbox1, i am using one gropubox1.

i tried below code after disabling the page. but, still i unable to do it..

int index = 0; // 1st tab
 (tablcontrol1.TabPages[index] as Control).Enabled = false; //Disable all the controls in the page
groupBox1.Enabled = true;

but, still i unable to do it..
As Éric mentioned, if your tab control that is a parent to all the children below [all the Groupboxes and other controls], is disabled then everything will be disabled. This is by design and cannot be bypassed. You could place the controls outside of the tab page and modify their visibility to achieve what you are looking for but it will all be un-manageable. If you can discuss your business requirements I am sure we can come up with a better solution.
???????
SOLUTION
Avatar of Éric Moreau
Éric Moreau
Flag of Canada 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
ASKER CERTIFIED SOLUTION
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
perfect answer...
       private void allControlsEnable(bool enable)
        {
            foreach (Control control in panelMain.Controls)
            {
                if ((control.Name != "ExcludeMe") && (control.Name != "ExcludeMeToo") && (control.Name != "ExcludeMeThree"))
                {
                    try
                    {
                        control.Enabled = enable;
                    }
                    catch
                    {
                        //Does not support enable
                    }
                }
            }
        }

Open in new window