title: How to set TextBox.TextAlign = MiddleLeft?
body: <p><a href="http://msdn.microsoft.com/en-us/library/system.windows.forms.label.textalign.aspx" rel="nofollow noreferrer"><code>Label.TextAlign</code></a> is of type <a href="http://msdn.microsoft.com/en-us/library/system.drawing.contentalignment.aspx" rel="nofollow noreferrer">ContentAlignment</a>. It allows developer to choose any combination of vertical and horizontal alignments.</p>
  
  <p><a href="http://msdn.microsoft.com/en-us/library/system.windows.forms.textbox.textalign.aspx" rel="nofollow noreferrer"><code>TextBox.TextAlign</code></a> is of type <a href="http://msdn.microsoft.com/en-us/library/system.windows.forms.horizontalalignment.aspx" rel="nofollow noreferrer">HorizontalAlignment</a>. It only allows <em>Left</em>, <em>Center</em> and <em>Right</em>.</p>
  
  <p>I would like to imitate behavior of <code>ContentAlignment = MiddleLeft</code> for a TextBox.</p>
  
  <p>Basically, I am trying to create a custom <code>MsgBox</code> dialog (<code>MessageBox.Show</code> for non-VB developers). Microsoft's <code>MsgBox</code> seems to have its <code>Label.TextAlign = MiddleLeft</code> automatically set through windows API. I need it to be a <code>TextBox</code> (not a Label), because users should be able to select text and copy to clipboard, partially or in full.</p>
  
  <p>So far I came up with this solution:</p>
  
  <ol>
  <li>Have a TableLayoutPanel, one of its cells will contain the above TextBox.</li>
  <li>Set <code>Anchor = Left,Right</code>. This will keep it stretched horizontally.</li>
  <li><p><code>MeasureText</code> just before showing (example: Form_Load), and adjust <code>TextBox.Height</code>.</p>
  
  <pre><code>Dim size As SizeF = TextBox1.CreateGraphics.MeasureString(
                      TextBox1.Text, TextBox1.Font, TextBox1.Width)
  TextBox1.Height = size.Height
  </code></pre></li>
  </ol>
  
  <p>Here is a picture of how it works in action (demo project, not yet a custom MsgBox):</p>
  
  <p><img src="https://i.stack.imgur.com/ZvgIO.png" alt="enter image description here"></p>
  
  <p>It seems like a rather dirty approach, not resilient to resizing and text changes. These events can be handled accordingly, of course, but it adds to the overall clutter.</p>
  
  <p><strong>Question: Is there a cleaner approach?</strong> I am looking for anything that may help implement the above behavior - it does not necessarily have to a be a <code>TextBox</code>.</p>