title: Autosize Form containing TabControl
body: <p>I have a Windows Form with a bunch of Controls and it works just fine. All the Controls are contained within a <code>TableLayoutPanel</code> which autosizes according to the Controls base-size and the <code>Form</code> 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.</p>
  
  <p>For instance, here's a simple example of how it looks like.
  The relevant code is:</p>
  
  <pre><code>//...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();
  </code></pre>
  
  <p><img src="https://i.stack.imgur.com/qrxim.jpg" alt="Example Dialog"></p>
  
  <p>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:</p>
  
  <pre><code>//...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();
  </code></pre>
  
  <p>So basically, instead of adding the <code>Panel</code> to the <code>Form</code>, I add it to the <code>TabPage</code> which is added to the <code>TabControl</code> which is added to the <code>Form</code>. However, this results in:
  <a href="https://i.stack.imgur.com/yxm2Z.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/yxm2Z.jpg" alt="Results"></a>
  </p>
  
  <p>The documentation states that the <code>AutoSize</code> property for <code>TabControl</code> and <code>TabPage</code> is mere infrastructure with no relevance. Most of the "solutions" I've found suggest using <code>.Dock = DockStyles.Fill</code>, which helped in that the <code>TabControl</code> now fills the Form as seen above instead of only occupying an even smaller part of it. The Form itself, however, remains unchanged.</p>
  
  <p>I've thought of using the <code>Panel</code>'s size and making the <code>TabControl</code>'s size equal to (or a function of) it, but I've noticed that the <code>Size</code> parameter apparently only changes when the <code>Panel</code> is painted, since adding Controls doesn't change it at all, so I'd have to wait for the <code>Panel</code> to paint and then resize it, which sounds sloppy. Is there a better solution?</p>
  
  <p><strong>UPDATE</strong>
  Using Anchoring and setting</p>
  
  <pre><code>Panel.Anchor = AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Top;
  tabControl.Anchor = AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Top;
  </code></pre>
  
  <p>Simply lead to this:</p>
  
  <p><a href="http://img803.imageshack.us/img803/3453/5112013125710pm.jpg" rel="nofollow noreferrer">http://img803.imageshack.us/img803/3453/5112013125710pm.jpg</a></p>