tags:

views:

27

answers:

2

I have a tab control with two tabs. Both tabs have controls which are unique to them, but there is one control which I would like to always appear on whichever tab is currently active.

I figure I just need to add some code to TabControl1_SelectedIndexChanged().

I tried

MyControl.Parent = TabControl1.TabPages(
                           TabControl1.TabPages.IndexOf(TabControl1.SelectedTab))
      MyControl.Parent.Update()   ' is this necessary? 

and I also tried

TabControl1.TabPages(
 TabControl1.TabPages.IndexOf(TabControl1.SelectedTab)).Controls.Add(SeMyControl)

but neither worked (the control moved once, but when I went back to the original tab, the control did not appear there.

googling found someone suggesting

TabControl1.TabPages(TabControl1.TabIndex).Controls.Add(MyControl)

but that looks dodgy as the control is never removed from the old tab, so repeated switching would probably add the control multiple times.

I feel that I am close, but not quite ... how do I do it?

+1  A: 

Using your second code snippet that you are concerned about because it doesn't remove it from the original tab, why not just remove it from the original tab before you add it to the new tab?

Maybe something like: TabControl1.TabPages(TabControl1.TabIndex).Controls.Remove(MyControl)

TaylorOtwell
Mawg
Yeah, I see what you're saying. I guess you could iterate through the tabs and check: TabPage.Contains(MyControl). If it does, then remove it and add it to your newly activated page.
TaylorOtwell
+1  A: 

No, that works fine since Controls.Add() changes the Parent property. Which automatically removes it from the tab page it was on before. A control instance can only have one parent.

The more straight-forward approach is to simply not put the control on a tab page but leave it parented to the form which a lower Z-order so it is always on top of the tab control. The only problem with that is that the designer will hassle you. It automatically sucks the control into the tab page when you move it on top of the tab control. One trick to fix that is to leave it off the tab control and change its Location property in the form constructor.

Hans Passant
+1 Sounds good, thanks. I guess that I can either, as you say eave it off the tab control and change its Location property in the form constructor, or don't have it in the designer at all and construct it dynamically in the form constructor.
Mawg
That's okay too, don't forget SetChildIndex() to move it in front.
Hans Passant