C# winform:从每个标签页访问用户控件中的控件 [英] C# winform: Accessing controls in a usercontrol from every tab page

查看:67
本文介绍了C# winform:从每个标签页访问用户控件中的控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用户控件,在这个控件中我有一堆文本框和标签.现在我已将此用户控件链接到另一个表单的选项卡控件.这是我正在使用的代码

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);

当我点击添加另一个标签"按钮时,上面的代码被执行并创建一个带有文本框的新标签页(类似于标签 1).

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?

提前致谢,游泳

推荐答案

我会为您正在搜索的控件添加一个标签并使用该方法:能够通过 Tag 属性查找 WinForm 控件

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
    }
}

或者递归遍历 Tab 控件

Or iterate recursively over the Tab controls

这篇关于C# winform:从每个标签页访问用户控件中的控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆