views:

275

answers:

3

I want to use a tab control to switch between sets of data displayed in a DataGridView (currently I'm using radio buttons). At runtime I can just move controls from one tab to the next, but the SelectedIndexChanged event doesn't fire in the designer.

private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
{
 tabControl1.TabPages[tabControl1.SelectedIndex].Controls.Add(label1);
}

Is there a way I can have the DGV appear on all my tabs in the designer, or is this a limitation I'll have to live with?

+2  A: 

Will the tab control switch any controls other than the DGV? Could you just lay the tab control down, adjust the vertical height so that it is only tall enough for the tabs themselves, and then put the DGV underneath it (so that it is not in the tab control)?

JMarsch
only works though if its only the grid but good solution
PoweRoy
That's a possibility, although I'm not sure I like the way it looks. Because the selected tab gets slightly wider when anything other than the last tab is selected there's a pixel or two of a 'stub' that is the border around the shrunk to zero size area for controls on the tabpage.
Dan Neely
A: 

I'm fairly sure that this is a limitation of the Visual Studio designer.

I'd go with something like JMarsh's solution too.

ChrisF
I've submitted screenshots of his solution and the standard configuration to the peanut gallery at work for comments. Since this is just a nuisance level issue in the designer, I'll probably go with whatever my boss decides he likes best.
Dan Neely
+1  A: 

I'm not sure I would view this a limitation, but as correct implementation. The tab page is a container and I'm pretty sure a widget can only have one immediate parent container. So the designer shouldn't be reassigning parents on the fly. It sounds like you don't really want the DGV to be in the tab page container but just to be able to interact with it as part of event handling for the Tab control.

If so, I would suggest you make use of panels and autosizing to handle the presentation issues mentioned by Dan Neely above.

For example, consider a search system with different "modes". Place a tab control in the top panel with tab pages containing widgets to implement search criteria secific to the "mode", e.g., keyword search vs attribute search vs workflow search. Place the DGV in a separate panel below the one containing the tabs. Then when there are tab events you can manipulate the DGV contents as you see fit. The panels can manage the presentation issues of resizing.

cdkMoose