循环遍历 TabControl 中的控件

发布于 2024-08-12 04:48:32 字数 246 浏览 6 评论 0原文

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

汽车, 宠物, 管理

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

所以,如果我在“汽车”选项卡上并单击“保存”,我只想循环浏览“汽车”选项卡上的控件,而不是“宠物”或“管理”选项卡上的控件。

怎样才能达到这样的结果呢?

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:

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.

How can achieve this result?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

往昔成烟 2024-08-19 04:48:33

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

这是关于 TabControl 的 MSDN 文章

例子:

        TabPage page = aTabControl.SelectedTab;

        var controls = page.Controls;

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

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

Here's an MSDN article on the TabControl.

Example:

        TabPage page = aTabControl.SelectedTab;

        var controls = page.Controls;

        foreach (var control in controls)
        {
            //do stuff
        }
苏辞 2024-08-19 04:48:33

我认为值得注意的是,一般来说,您应该对您的应用程序采取更结构化的方法。例如,不是将所有控件都放在三个选项卡页上,而是在每个选项卡页上仅包含一个 UserControl。例如,CarUserControlPetUserControlAdminUserControl 然后每个用户控件都知道如何创建正确的各自数据结构,因此您无需手动创建使用选项卡间循环等在同一抽象级别将所有内容组合在一起。

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

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 AdminUserControl e.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.

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.

以酷 2024-08-19 04:48:33

我想在我编写的应用程序的特定选项卡中获取 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();
    }
}

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

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();
    }
}
云柯 2024-08-19 04:48:33

Controls 属性是正确的选择...

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

The Controls property is the way to go...

foreach(Control c in currentTab.Controls)
{
    if(c is TextBox)
        // check for text change
    if(c is CheckBox)
        //check for check change
    etc...
}
合约呢 2024-08-19 04:48:33

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

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

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

foreach(Control c in tabControl.SelectedTab.Controls)
{
    //do checks
}
这个俗人 2024-08-19 04:48:33
foreach (Control c in this.tabControl1.SelectedTab.Controls)
{
  // Do something
}
foreach (Control c in this.tabControl1.SelectedTab.Controls)
{
  // Do something
}
渡你暖光 2024-08-19 04:48:33

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

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

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);
            }
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文