Dynamically Resizing a Rich Text Box Control

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi

I have a user control that contains a RichTextBox in vb.net. In my
program, I create multiple instances of this control (with the
RichTextBox being in each one), that appear one above the other on a
panel. The problem is that each control may have varying amounts of
data, and so each RichTextBox (within each control) needs to size to
fit the amount of text within that RichTextBox. I've tried
multiplying the size of a single line times the number of lines in the
RichTextBox but that doesn't seem to work. Any help would be
fantastic!!!

If there is any other control (other than RichTextBox) which might
handle this better, or perhaps a third party control, I would love to
hear about it!!!!

Thanks SO much in advance!!
 
Hi,

Maybe this will point you in right direction.

Private Sub RichTextBox1_TextChanged(ByVal sender As Object, ByVal e As
System.EventArgs) Handles RichTextBox1.TextChanged

Dim g As Graphics = RichTextBox1.CreateGraphics

Dim h As Integer

h = g.MeasureString(RichTextBox1.Text, RichTextBox1.Font,
RichTextBox1.Width).Height + 10

If h > RichTextBox1.Height Then RichTextBox1.Height = h

End Sub


Ken
 
Thanks a lot Ken. Your solution almost worked. However, there are still a few lines of text at the bottom of the control that are cut off. Can anyone give me more information on the parameters of the graphics.measureString function? I tried hard coding some extra pixles of height which worked to a point. larger enteries were still cut off. My rich text box has bold and regular text style with the default font. Thanks in advance to anyone who can solve my problem!
 
Back
Top