3

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();

Example Dialog

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: Results

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:

http://img803.imageshack.us/img803/3453/5112013125710pm.jpg

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Wasabi
  • 2,879
  • 3
  • 26
  • 48
  • Unrelated comment: Your GUI seems to be some kind of Plan drawing or diagram designer application. I STRONGLY suggest you use relevant current UI technologies for this and leave winforms behind. [`Here`](http://stackoverflow.com/a/15821573/643085) is an example of a similar thing done in WPF + MVVM, supporting animations, mouse drag, animations, and other very interesting visual and interactivity features. – Federico Berasategui May 11 '13 at 15:56
  • Not to mention the problem you're having here (which is the lack of resolution independence of winforms) absolutely DOES NOT EXIST in newer technologies. – Federico Berasategui May 11 '13 at 16:02
  • 1
    Yeah, I looked at that link and... that looks pretty cool. Thank you for making me spend the rest of the day learning WPF. Also... you're really passionate about WPF, I can tell. – Wasabi May 11 '13 at 16:18

2 Answers2

6

Instead of setting sizes, why not take advantage of Anchoring? If you anchor the Panel to Top | Left | Right | Bottom of the TabPage/TabControl, then the TabControl to the Form the same way it's only ever the Form's size you need to worry about. It will dictate the sizes of all the other items anchored to it.

Adrian
  • 2,825
  • 1
  • 22
  • 32
  • Well, isn't the `Panel` anchored to its host object by default? The default value of `.Anchor = Top | Left` and I haven't modified that. As well, my idea was precisely the opposite, to let the contents of the Form decide their own size (via `.Autosize`) and then the `Form` autosizes to its content. I don't want to worry about the different possible sizes of fonts and therefore of the whole `Form` on different monitors or on different versions of Windows. – Wasabi May 11 '13 at 15:21
  • Everything is anchored `Top | Left` by default. My suggestion (which I will edit the answer to articulate better) was that you anchor `Top | Left | Right | Bottom`. Lock them all to the size of the `Form`, then only worry about setting the size of the `Form` from its contents. – Adrian May 11 '13 at 15:28
  • Thanks, but oddly that actually made things worse. Adding: `Panel.Anchor = AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Top;` `tabControl.Anchor = AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Top;` Just made the `TabControl` behave as if `.Dock != Fill`, and now it doesn't even fill the Form. And its a real bummer that apparently there isn't a way to make the `TabControl` behave as nicely as a simple `Form` with a `Panel`, where I didn`t have to worry about the size of anything. – Wasabi May 11 '13 at 15:43
-1

use .AutoScaleMode = None in designer for your panel as usercontrol. In my case TabControl-TabPage-myPage it works.

Andrey
  • 1
  • 1