C# 循环遍历 TabControl 中的控件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1744429/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 20:25:08  来源:igfitidea点击:

Loop through controls in TabControl

c#winformsloopsforeachtabcontrol

提问by Sesame

I am looking for a way to loop through controls on a particular tab of a tabcontrol. For example, I have a tabcontrol with the following tabs:

我正在寻找一种方法来循环浏览 tabcontrol 的特定选项卡上的控件。例如,我有一个带有以下选项卡的 tabcontrol:

Cars, Pets, Admin

汽车、宠物、管理员

On each of these tabs are several controls to display/edit/save data, etc. On the "Save" button, I would like to loop through the controls for that particular tab to check whether all required fields have been filled in.

在每个选项卡上都有几个用于显示/编辑/保存数据等的控件。在“保存”按钮上,我想遍历该特定选项卡的控件以检查是否已填写所有必填字段。

So, if I am on the Cars tab and click "Save," I want to loop ONLY through the controls on the Cars tab and NOT the Pets or Admin tabs.

因此,如果我在 Cars 选项卡上并单击“Save”,我只想循环通过 Cars 选项卡上的控件而不是 Pets 或 Admin 选项卡。

How can achieve this result?

怎样才能达到这个结果?

采纳答案by Joseph

As for looping through a TabControl's controls, you need to use the Controls property.

至于循环访问 TabControl 的控件,您需要使用 Controls 属性。

Here's an MSDN articleon the TabControl.

这是关于 TabControl的MSDN 文章

Example:

例子:

        TabPage page = aTabControl.SelectedTab;

        var controls = page.Controls;

        foreach (var control in controls)
        {
            //do stuff
        }

回答by Sesame

Example where I wanted to get the DataGridView in a particular tab for an application I wrote.

我想在我编写的应用程序的特定选项卡中获取 DataGridView 的示例。

TabPage pg = tabControl1.SelectedTab;

// Get all the controls here
Control.ControlCollection col = pg.Controls;

// should have only one dgv
foreach (Control myControl in col)
{
    if (myControl.ToString() == "System.Windows.Forms.DataGridView")
    {
        DataGridView tempdgv = (DataGridView)myControl;   
        tempdgv.SelectAll();
    }
}

回答by Lee

TabControl has a SelectedTab property, so you'd do something like this:

TabControl 有一个 SelectedTab 属性,因此您可以执行以下操作:

foreach(Control c in tabControl.SelectedTab.Controls)
{
    //do checks
}

回答by Philip Wallace

foreach (Control c in this.tabControl1.SelectedTab.Controls)
{
  // Do something
}

回答by Ragepotato

The Controlsproperty is the way to go...

Controls物业是要走的路...

foreach(Control c in currentTab.Controls)
{
    if(c is TextBox)
        // check for text change
    if(c is CheckBox)
        //check for check change
    etc...
}

回答by Greg D

I feel it's important to note that, in general, you should take a more structured approach to your application. E.g., instead of having all the controls on three tab pages, include exactly one UserControl on each tabpage. A CarUserControl, PetUserControl, and AdminUserControle.g. Then each user control knows how to create the proper respective data structure so you don't have to manually munge it all together at the same level of abstraction using inter-tab loops and whatnot.

我觉得重要的是要注意,一般来说,您应该对您的应用程序采取更结构化的方法。例如,不是将所有控件都放在三个标签页上,而是在每个标签页上只包含一个 UserControl。A CarUserControl, PetUserControl, and AdminUserControleg 然后每个用户控件都知道如何创建正确的相应数据结构,因此您不必使用标签间循环等在同一抽象级别手动将它们全部混合在一起。

Such a separation of concerns will make it much easier to reason about your program and is good practice for writing maintainable code for your future career.

这种关注点的分离将使您更容易推理您的程序,并且是为您未来的职业编写可维护代码的好习惯。

回答by kenny

I had the need to disable or enable controls of a tab as well. I had to go a bit more generic though. Hope it helps people and I didn't make a mistake

我还需要禁用或启用选项卡的控件。不过,我不得不更通用一些。希望它可以帮助人们,我没有犯错

    private void toggleControls(Control control, bool state)
    {
        foreach (Control c in control.Controls)
        {
            c.Enabled = state;
            if (c is Control)
            {
                toggleControls(c, state);
            }
        }
    }