I’ve just discovered that TabPages don’t resize with a TabControl until after they’re selected. This is a big problem for me because I have a panel that’s anchored on all four edges and shared – by setting its Parent property – between two of several pages on a TabControl that is fill-docked to a resizable form. So, if I select a tab that never contains the panel, resize the form, and then select a tab that will contain it but doesn’t presently, the panel doesn’t get resized.
I’ve found that fill-docking the panel rather than anchoring it works okay, but I’d rather avoid that if at all possible, as the shared panel doesn’t extend to the edges of the pages and its size can vary, so I’d have to mess around with the padding size of each page rather than simply altering the panel’s size.
I’ve tested this in both VB and C# and they both behave the same way. I thought the solution given for this known bug might do the trick, but unfortunately it doesn’t.
Is there anything else I can do, such as force the pages to resize, to get around this problem?
EDIT – Steps to reproduce this in VS 2010:
-
Add a TabControl to a resizable form, fill-dock it and add a third tab.
-
Add a Panel to the 3rd tab, leaving plenty of space at the sides, anchor it to all four edges and set its BackColor to something different than the tabPage’s so you can see it at runtime.
-
Add this line of code to the
SelectedIndexChangedevent:
VB
If TabControl1.SelectedIndex > 0 Then Panel1.Parent = TabControl1.SelectedTab
C#
if (tabControl1.SelectedIndex > 0) panel1.Parent = tabControl1.SelectedTab;
-
Run/debug the form and resize it whilst the 1st tab is selected.
-
Select the 2nd tab – you will see the panel hasn’t resized.
As a workaround Hans Passant suggested I put the panel on the first TabPage (hidden) then set its Visible property in the event handler. As I explained, that wouldn’t quite work – as it isn’t just a “once-only” problem at start up; it occurs whenever the form is resized while a non-panel-displaying tab is selected, then selecting the panel-displaying tab that doesn’t currently contain the panel – but it did point me in the right direction for the answer. Which is to set the panel’s Visible property according to which tab is selected and always move the panel to the selected tab, regardless of whether the panel is displayed on it – EG: