<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
		>
<channel>
	<title>Comments on: Auto Height TextBox</title>
	<atom:link href="http://savij.com/2008/02/11/auto-height-textbox/feed/" rel="self" type="application/rss+xml" />
	<link>http://savij.com/2008/02/11/auto-height-textbox/</link>
	<description>Attacking Programming Obstacles</description>
	<lastBuildDate>Fri, 22 Oct 2010 20:43:54 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
	<item>
		<title>By: savij</title>
		<link>http://savij.com/2008/02/11/auto-height-textbox/#comment-405</link>
		<dc:creator><![CDATA[savij]]></dc:creator>
		<pubDate>Wed, 12 Nov 2008 15:52:09 +0000</pubDate>
		<guid isPermaLink="false">http://savij.com/?p=296#comment-405</guid>
		<description><![CDATA[Thats a great solution! You are right, the size did get off on longer resizes. I am not going to convert this to VB (as I do both). But for those needing it, just convert the DllImport line. The rest is just removing the semicolons and change != to . 

I also stole this code from a friend, I have not tested it, but he says it works correctly.

private void InfoQuestionUserControl_Paint(object sender, PaintEventArgs e)

    {

      SuspendLayout();

      double numberOfRows = 1;

      if (_painted == false)

      {

        SizeF size = e.Graphics.MeasureString(Question, _lblInfoQuestion.Font);

 

        if (size.Width &gt; _lblInfoQuestion.Width)

        {

          numberOfRows = Math.Floor(size.Width/_lblInfoQuestion.Width);

 

          if ((_lblInfoQuestion.Width % size.Width) &gt; 0)

          {

            numberOfRows++;

          }

        }

 

        _lblInfoQuestion.Height = (int)(numberOfRows * _lblInfoQuestion.Height);

 

        Height = _lblInfoQuestion.Height + 2;

        _painted = true;

      }

      ResumeLayout();

    }

Hope this helps!!!

Thanks for the comment Stuart!

-Jeff]]></description>
		<content:encoded><![CDATA[<p>Thats a great solution! You are right, the size did get off on longer resizes. I am not going to convert this to VB (as I do both). But for those needing it, just convert the DllImport line. The rest is just removing the semicolons and change != to . </p>
<p>I also stole this code from a friend, I have not tested it, but he says it works correctly.</p>
<p>private void InfoQuestionUserControl_Paint(object sender, PaintEventArgs e)</p>
<p>    {</p>
<p>      SuspendLayout();</p>
<p>      double numberOfRows = 1;</p>
<p>      if (_painted == false)</p>
<p>      {</p>
<p>        SizeF size = e.Graphics.MeasureString(Question, _lblInfoQuestion.Font);</p>
<p>        if (size.Width &gt; _lblInfoQuestion.Width)</p>
<p>        {</p>
<p>          numberOfRows = Math.Floor(size.Width/_lblInfoQuestion.Width);</p>
<p>          if ((_lblInfoQuestion.Width % size.Width) &gt; 0)</p>
<p>          {</p>
<p>            numberOfRows++;</p>
<p>          }</p>
<p>        }</p>
<p>        _lblInfoQuestion.Height = (int)(numberOfRows * _lblInfoQuestion.Height);</p>
<p>        Height = _lblInfoQuestion.Height + 2;</p>
<p>        _painted = true;</p>
<p>      }</p>
<p>      ResumeLayout();</p>
<p>    }</p>
<p>Hope this helps!!!</p>
<p>Thanks for the comment Stuart!</p>
<p>-Jeff</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Stuart McConnachie</title>
		<link>http://savij.com/2008/02/11/auto-height-textbox/#comment-404</link>
		<dc:creator><![CDATA[Stuart McConnachie]]></dc:creator>
		<pubDate>Wed, 12 Nov 2008 13:51:43 +0000</pubDate>
		<guid isPermaLink="false">http://savij.com/?p=296#comment-404</guid>
		<description><![CDATA[Nice, but this code has a problem.  I believe MeasureString spaces each line exactly using a float value, whereas the control uses the same precise integer number of pixels between each line.  This introduces rounding errors as the number of lines in the control grows.  The result is that the returned hight is no longer sufficient to contain all the text as drawn by the control and you lose the top or bottom one or more lines:

Here&#039;s a workaround (in C# sorry, but should be easy to convert ).  This produces EXACTLY the right height of control.  It also accounts for line wrap and empty text boxes:

private const int EM_GETLINECOUNT = 0xBA;

[DllImport(&quot;user32.dll&quot;, CharSet = CharSet.Auto, SetLastError = false)]
static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);

private void ReSizeTextBox()
{
	int iRequiredRows = SendMessage(this.textBox.Handle, EM_GETLINECOUNT, IntPtr.Zero, IntPtr.Zero).ToInt32();
	int iRequiredHeight = iRequiredRows * this.textBox.Font.Height;
	int iDiffHeight = iRequiredHeight - this.textBox.ClientSize.Height;
	if (iDiffHeight != 0)
	{
		this.textBox.Height += iDiffHeight;
	}
}

Now  just call the above OnTextChanged and OnClientSizeChanged for the TextBox and you are done.]]></description>
		<content:encoded><![CDATA[<p>Nice, but this code has a problem.  I believe MeasureString spaces each line exactly using a float value, whereas the control uses the same precise integer number of pixels between each line.  This introduces rounding errors as the number of lines in the control grows.  The result is that the returned hight is no longer sufficient to contain all the text as drawn by the control and you lose the top or bottom one or more lines:</p>
<p>Here&#8217;s a workaround (in C# sorry, but should be easy to convert ).  This produces EXACTLY the right height of control.  It also accounts for line wrap and empty text boxes:</p>
<p>private const int EM_GETLINECOUNT = 0xBA;</p>
<p>[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)]<br />
static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);</p>
<p>private void ReSizeTextBox()<br />
{<br />
	int iRequiredRows = SendMessage(this.textBox.Handle, EM_GETLINECOUNT, IntPtr.Zero, IntPtr.Zero).ToInt32();<br />
	int iRequiredHeight = iRequiredRows * this.textBox.Font.Height;<br />
	int iDiffHeight = iRequiredHeight &#8211; this.textBox.ClientSize.Height;<br />
	if (iDiffHeight != 0)<br />
	{<br />
		this.textBox.Height += iDiffHeight;<br />
	}<br />
}</p>
<p>Now  just call the above OnTextChanged and OnClientSizeChanged for the TextBox and you are done.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: savij</title>
		<link>http://savij.com/2008/02/11/auto-height-textbox/#comment-374</link>
		<dc:creator><![CDATA[savij]]></dc:creator>
		<pubDate>Fri, 29 Feb 2008 13:37:40 +0000</pubDate>
		<guid isPermaLink="false">http://savij.com/?p=296#comment-374</guid>
		<description><![CDATA[No I don&#039;t but it should only take 5 or 10 minutes to convert it. I don&#039;t see any vb specific code in there, so it&#039;s really just reformatting the syntax to c#.]]></description>
		<content:encoded><![CDATA[<p>No I don&#8217;t but it should only take 5 or 10 minutes to convert it. I don&#8217;t see any vb specific code in there, so it&#8217;s really just reformatting the syntax to c#.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Cheetos72</title>
		<link>http://savij.com/2008/02/11/auto-height-textbox/#comment-373</link>
		<dc:creator><![CDATA[Cheetos72]]></dc:creator>
		<pubDate>Thu, 28 Feb 2008 21:26:34 +0000</pubDate>
		<guid isPermaLink="false">http://savij.com/?p=296#comment-373</guid>
		<description><![CDATA[NICE -- do you have it in C#?]]></description>
		<content:encoded><![CDATA[<p>NICE &#8212; do you have it in C#?</p>
]]></content:encoded>
	</item>
</channel>
</rss>

