I have a Windows Form with a bunch of Controls and it works just fine. All the Controls are contained within a TableLayoutPanel
which autosizes according to the Controls base-size and the Form
autosizes according to the panel, so I don't really have to worry about sizes in different platforms and computers since everything should resize according the the current configuration's settings.
For instance, here's a simple example of how it looks like. The relevant code is:
//...code defining all the other Commands and .Add()'ing them to the Panel
form.Controls.Add(Panel);
form.AutoSize = true;
form.FormBorderStyle = FormBorderStyle.FixedDialog;
form.ShowDialog();
I now want to get this entire panel and place it within a TabControl so that it is just one tab amonst many others. I did this as follows:
//...code defining all the other Commands and .Add()'ing them to the Panel
TabControl tabControl = new TabControl();
tabControl.Dock = DockStyle.Fill;
TabPage tabPage = new TabPage("C1");
tabPage.Controls.Add(Panel);
tabControl.TabPages.Add(tabPage);
form.Controls.Add(tabControl);
form.AutoSize = true;
form.FormBorderStyle = FormBorderStyle.FixedDialog;
form.ShowDialog();
So basically, instead of adding the Panel
to the Form
, I add it to the TabPage
which is added to the TabControl
which is added to the Form
. However, this results in:
The documentation states that the AutoSize
property for TabControl
and TabPage
is mere infrastructure with no relevance. Most of the "solutions" I've found suggest using .Dock = DockStyles.Fill
, which helped in that the TabControl
now fills the Form as seen above instead of only occupying an even smaller part of it. The Form itself, however, remains unchanged.
I've thought of using the Panel
's size and making the TabControl
's size equal to (or a function of) it, but I've noticed that the Size
parameter apparently only changes when the Panel
is painted, since adding Controls doesn't change it at all, so I'd have to wait for the Panel
to paint and then resize it, which sounds sloppy. Is there a better solution?
UPDATE Using Anchoring and setting
Panel.Anchor = AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Top;
tabControl.Anchor = AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Top;
Simply lead to this: