sharp12345
sharp12345

Reputation: 4498

Auto resize the textbox so that scroll bars are not needed (or detect actual number of lines after wrapping occurs)

I need to resize my form based on the textbox contents. Currently, I can't reliabley detect the actual number of lines in the textbox.text. All I can do is count the occurrence of the \n character, but that does not take into account automatic word wrapping.

And the characters do not have a fixed width, so I can't predict where new lines will occur.

Is it possible to detect the textbox's vertical-scroll bar value/min/max, so that I can know how many hidden lines there are? (and increase the textbox size until there to eliminate scrolling)

I have the textbox with:

textBox1.Multiline = true;
textBox1.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;

Upvotes: 0

Views: 860

Answers (2)

Fooksie
Fooksie

Reputation: 480

If you are using WPF, you could set the width of columns and rows in a Grid layout panel to "Auto" and have it determine the size you need, alternatively if you're using WinForms perhaps something like this would help?

VB.NET: Finding the size of text to be drawn, before it is drawn to a control

Upvotes: 0

Your best bet is probably to use TextRenderer.MeasureText to calculate the size of the text and then resize your control accordingly.

Upvotes: 1

Related Questions