Autosize Textbox or RichTextBox

G

Guest

I have a need to have a Textbox or RTF box autosize to content. In other
words, as the user types, the textbox grows vertically to show all the lines
rather than show scrollbars.

I was under the impression that this ability was going to be added in
VS2005, but it certainly isn't obvious to me.

Any ideas on how to accomplish this would be appreciated.
 
G

Guest

Multiline does allow the user to type multiple lines of text. Autosize,
however, only changes the control size when the font changes, and on both the
RichTextBox and TextBox it says "This property is not relevant to this
class." I need the control to size vertically as the user enters text at
runtime.

Todd

Peter Oliphant said:
I think studying these should do the trick:

http://msdn2.microsoft.com/en-us/library/system.windows.forms.textboxbase.autosize.aspx
http://msdn2.microsoft.com/en-us/library/system.windows.forms.textbox.multiline.aspx

[==P==]

Brewski said:
I have a need to have a Textbox or RTF box autosize to content. In other
words, as the user types, the textbox grows vertically to show all the
lines
rather than show scrollbars.

I was under the impression that this ability was going to be added in
VS2005, but it certainly isn't obvious to me.

Any ideas on how to accomplish this would be appreciated.
 
G

Guest

OK, I finally figured out the answer. The key is handling the
ContentsResized event on the RichTextBox (doesn't exist on a TextBox). The
ContentsResizedEventArgs has a NewRectangle property which will give you the
desired size. Here is what I have:

private void rtb_ContentsResized(object sender. ContentsResizedEventArgs e)
{
(RichTextBox)sender.Height = e.NewRectangle.Height + 5;
}

So, there is the solution if anyone ever wants to know.

Brewski AKA Todd Brewer.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top