简体   繁体   中英

C# winform: Accessing controls in a usercontrol from every tab page

I have a user control and in this control i have a bunch of text boxes and labels. Now I have linked this user control to another form's tab control. Here is the code I am using

TabPage tp  = new TabPage();
tp.Controls.Add(TipUserControl);
tp.Text = "Tab "+ tabctrl_Fields.TabCount + 1;
tabctrl_Fields.TabPages.Add(tp);

When I click on a "Add another tab" button, the above code gets executed and a new tab page with the text boxes (similar to Tab 1) is created.

Now what I am looking for is When the user click on "Done" button in the form (not in the user control), it should be able to loop through every tab and every control (textboxes, labels etc) within that tab. Can anyone suggest on how to write this code?

Thanks in advance, Swamy

I would add a Tag to the controls that you are searching for and use that approach: Ability to find WinForm control via the Tag property

private void FindTag(Control.ControlCollection controls)
{
    foreach (Control c in controls)
    {
        if (c.Tag != null)
        //logic

       if (c.HasChildren)
           FindTag(c.Controls); //Recursively check all children controls as well; ie groupboxes or tabpages
    }
}

Or iterate recursively over the Tab controls

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM