this.controls not including the controls inside tabcontrol
Hi All,
I tried to get all the buttons in the working form using the code below, however, i found all the buttons in the Tabcontrol is not included in this.Controls. So how can i access all controls in a form?
foreach (Control childControl in this.Controls )
{
if (childControl is Button)
{
childControl.Enabled = inStatus;
}
}
Thanks
Re: this.controls not including the controls inside tabcontrol
controls located on a tab are added to that tab's controls collection, not the form's control collection.
iterate over your tab control's tab pages property, and check each page's controls collection.
Re: this.controls not including the controls inside tabcontrol
Quote:
Originally Posted by
MadHatter
controls located on a tab are added to that tab's controls collection, not the form's control collection.
iterate over your tab control's tab pages property, and check each page's controls collection.
Basically for knowing about where controls have been added, you can always look into the designer.cs of a form, because there you can read the code how your conrols are created and where they are added to. :wave:
Re: this.controls not including the controls inside tabcontrol
this is the case for all of the container controls.
Re: this.controls not including the controls inside tabcontrol
Thanks guys, i worked out this with following code. Cheers,
foreach (Control childControl in this.Controls )
{
if (childControl is TabControl)
{
foreach (Control ctlTabPage in childControl.Controls)
{
foreach (Control innerControl in ctlTabPage.Controls)
{
if (innerControl is Button)
{
innerControl.Enabled = inStatus;
}
}
}
}
else
{
if (childControl is Button)
{
childControl.Enabled = inStatus;
}
}
}
Re: this.controls not including the controls inside tabcontrol
Controls have a HasChildren property. Instead of explicitly checking for a tab control, just check the HasChildren property of each control.
Re: this.controls not including the controls inside tabcontrol
Great! thank you...i will try it