PC Review


Reply
Thread Tools Rate Thread

Auto-adjust height of richtextbox (or textbox) based on length of string?

 
 
papalarge
Guest
Posts: n/a
 
      31st Jan 2007
Hey all...

I've been using the following to calculate the height the textbox
needs to be in order to grow it vertically to its necessary size.

textbox1.Height = Me.CreateGraphics().MeasureString(textbox1.Text,
textbox1.Font, textbox1.Width).Height

However it's not appearing to work that well, especially when I'm
using a RichTextbox control, and adjusting it (or checking it) on each
KeyDown event.

Private Sub richtextbox1_KeyPress(ByVal sender As Object, ByVal e As
System.Windows.Forms.KeyPressEventArgs) Handles RichTextBox1.KeyPress
' ADJUSTS HEIGHT OF LABEL BASED ON AMOUNT OF TEXT
RichTextBox1.Height =
Me.CreateGraphics().MeasureString(RichTextBox1.Text,
RichTextBox1.Font, RichTextBox1.Width).Height
End Sub

Any other methods of determining this? In the end, I'd like a
richtextbox control that grows vertically (only) as the text wraps to
a second line (and only grows at that point, no sooner)

Thanks...

 
Reply With Quote
 
 
 
 
papalarge
Guest
Posts: n/a
 
      31st Jan 2007
Wow... found the answer, was easier than I thought. It takes into
account at least bold, underline text. Not sure about tables, etc.
though.

Private Sub RichTextBox1_ContentsResized(ByVal sender As Object, ByVal
e As System.Windows.Forms.ContentsResizedEventArgs) Handles
RichTextBox1.ContentsResized
' THE "10" IS FOR THE SIZE DIFFERENCE BETWEEN THE HEIGHT OF YOUR FONT
AND THE PADDING YOU'D LIKE TO GIVE IT
If RichTextBox1.Height <> e.NewRectangle.Height + 10 Then
RichTextBox1.Height = e.NewRectangle.Height + 10
End If
End Sub

 
Reply With Quote
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Row Height-Auto Adjust to Content? Neon520 Microsoft Excel Misc 5 21st Dec 2009 11:14 PM
auto adjust column widths and row height DebC Microsoft Excel Misc 1 24th Nov 2009 03:17 PM
Re: Row height to auto-adjust for wrapped text Gord Dibben Microsoft Excel Misc 2 20th Dec 2006 05:00 PM
Auto adjust label height =?Utf-8?B?c3luYw==?= Microsoft Dot NET Compact Framework 0 17th Jun 2006 07:58 PM
Row Auto Height Adjust Dewayne Microsoft Excel Worksheet Functions 3 20th Aug 2004 10:14 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 04:56 PM.