title: Determine Control's 'AutoSize'-size at runtime
body: <p>I want to layout controls during runtime (dynamically created). For the purpose of this question, let's restrict to a Button control. I want to set the control's properties (such as <code>Text</code>) and then determine the minimum size for the control for it to display properly; the size that setting <code>AutoSize = true</code> would give. In C# example code, with <code>GetAutoSizeSize</code> being this minimum size:</p>
  
  <pre><code>Button button = new Button();
  this.Controls.Add(button);
  
  button.Text = "Example";
  button.Size = GetAutoSizeSize(button);
  button.Location = /* Some calculation based on button.Size */
  </code></pre>
  
  <hr>
  
  <h3>Possible solution: AutoSize</h3>
  
  <p>One can set <code>button.AutoSize = true</code> and <code>button.AutoSizeMode = AutoSizeNode.GrowAndShrink</code>. After that, the <code>button.Size</code> can be fetched, after which <code>AutoSize</code> can be turned off and the control size can be changed.</p>
  
  <p>Potential issues:</p>
  
  <ul>
  <li>It looks odd and I can't help but feel that this could easily break, but maybe I am wrong?</li>
  </ul>
  
  <h3>Possible solution: GetPreferredSize</h3>
  
  <p><code>button.GetPreferredSize</code> can be used to get a size that the control wants to be.</p>
  
  <p>Problems with this:</p>
  
  <ul>
  <li>Its usage is internal and/or meant for flow layout.</li>
  <li><code>GetPreferredSize</code> takes a suggested size as a parameter, so one needs to guess at what would be appropriate.</li>
  <li>The size returned is wrong, in that it returns the 'comfy' size of a control, which can be much larger than the minimum size that <code>AutoSize</code> gives.</li>
  </ul>
  
  <hr>
  
  <p><strong>EDIT:</strong> From the comments and some trial-and-error, I was able to conclude that the problems I originally listed with the AutoSize-method were due to needing both the control to be added to the control collection first and <code>AutoSizeMode</code> set to <code>GrowAndShrink</code>.</p>
  
  <p>I would like to know if there is a function (and/or more 'robust' way) of determining the AutoSize-size: a function like <code>GetPreferredSize</code> that returns the size without actually having to toggle <code>AutoSize</code>.</p>