views:

595

answers:

6

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?

+1  A: 

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();
    }
}
0A0D
Why so complex? Also, you can do 'if (myControl is DataGridView)'
Philip Wallace
How is this complex? It has multiple tabs and I want a particular control in the tab (i.e DGV).
0A0D
Maybe complex was the wrong word. Why do you have pg and col? They are not reused...
Philip Wallace
They are later. Plus, you can just dispose. Not a big deal.
0A0D
@Xaero: Whether or not you declare them, the compiler is going to have to create a temp variable to store the memory somewhere if you use a less variable specific example.
0A0D
Thanks Greg - I can see that. The point is, there is no need to create a variable for these - but as Brian points out, this is just a snippet of a larger codebase.
Philip Wallace
@Brian - I'm not sure about that, what if you had just used tabControl1.SelectedTab.Controls?Anyway, my original point is irrelevant as we cannot see the entire piece of code.
Philip Wallace
@Xaero: That example is a compressed version of mine above. Memory is being allocated equally in both cases. pg variable and col variable are equally allocated in both cases. Yours just hides simple implementation and is less lines.
0A0D
+4  A: 

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 = pg.Controls;

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

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

foreach(Control c in tabControl.SelectedTab.Controls)
{
    //do checks
}
Lee
A: 
foreach (Control c in this.tabControl1.SelectedTab.Controls)
{
  // Do something
}
Philip Wallace
A: 

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...
}
Ragepotato
+7  A: 

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.

Greg D
Yes, good advice and a better answer.
Ed Swangren
I marked Joseph's response as the answer, but gave you +1 for the valuable advice. Thanks!
Robert
We use this approach, utilizing a customized UserControl that is a container that can be loaded, with the click of a button built into the container, into its own separate window when convenient for the user. There may be third-party controls that do this as well.
PaulR