简体   繁体   English

C#winform:从每个选项卡页访问用户控件中的控件

[英]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. 当我单击“添加另一个标签”按钮时,将执行上述代码,并创建一个带有文本框(类似于标签1)的新标签页。

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 我将Tag添加到要搜索的控件中,并使用该方法: 能够通过Tag属性找到WinForm控件。

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 或递归地遍历Tab控件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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