Click here to Skip to main content
15,916,692 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have a textbox which changes size depending on amount of lines in it, These lines are read from a text file, and then appended to a richtextbox (Before you ask, I require text formatting)

After each append, I have set a line count to increase after each run of the append function.

Once everything has been appended to the textbox, it hits an end while line isnot nothing, it then runs the function to adjust the currenttb.

I can't figure out why the tb doesn't seem the same size all the time

The font size as the text is being appended is 12.

I thought to set the line to be main.linecount * 12. 12 being the size of the font as it's appended, but this doesn't work. size is much to small.
If I go for 22, it suits for about 18 lines, where as if there are 22, the textbox is too big. If I only have 5 lines, the box is too small.

What I have tried:

the set textbox height is done as per below

Private Sub txtboxheight(ByVal linecount)
If currenttb.InvokeRequired Then
currenttb.Invoke(New Action(Of String)(AddressOf txtboxheight), linecount)
Else

currenttb.Height = main.linecount * 22
End If
main.linecount = 0
End Sub
Posted
Updated 29-May-16 1:33am

This works reasonably well...
I populated a RichTextBox
VB
Dim sb As StringBuilder = New StringBuilder("")
For i = 1 To 20
    sb.AppendLine("Lorem Ipsum etc")
Next
sb.Length -= Environment.NewLine.Length 'Remove final newline
RichTextBox1.Text = sb.ToString()

txtboxheight(RichTextBox1)

This is my interpretation of the txtboxheight routine
VB.NET
Private Sub Txtboxheight(rtb As RichTextBox)
    Dim rtbSize As Size = TextRenderer.MeasureText(RichTextBox1.Text, RichTextBox1.Font)
    RichTextBox1.Width = rtbSize.Width + (2 * RichTextBox1.Font.SizeInPoints)
    RichTextBox1.Height = rtbSize.Height + RichTextBox1.Font.SizeInPoints
End Sub

MSDN reference - TextRenderer Class (System.Windows.Forms)[^]

Things to consider:
This assumes that all the lines are the same height - if you have added formatting then this might not be the case. You could possibly use the .Rtf property but if you do then the Width is not calculated properly.

For the addition of the "padding" at the bottom I've used the Font size (in points) as an arbitrary buffer - you might want to play around with that. Similarly for the width I used twice the font size which worked for all the fonts I tried.

If you don't adjust the width then be aware than having WordWrap = True will completely break the calculation
 
Share this answer
 
Comments
Mendaharin 29-May-16 7:47am    
Fricking wordwrap!!! I'll still look into using the above code, as it looks like it will work better than mine, but mine nearly worked, just I had 3 or 4 lines that wordwrapped and didn't really notice it til I had a play around with the width of the textbox and everything started sitting correctly.

Another rookie mistake on my behalf

Cheers M8 :)
A font height is usually given in points, each of which is 1/72 inches. So you need to convert that to pixels, adjusted to the pixel density of your screen. See c# - Convert Pixels to Points - Stack Overflow[^].
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900