Determine how wide a textbox or text area should be to hold three lines

283 views Asked by At

I'm trying to figure out how to size a text box to closely fit three lines of wrapped text. I'm working in vb6, but I'll accept an answer in any language if I can understand it.

The problem is that I have a flexgrid with long headings and room for three lines of text in them, and I want to resize the text area to exactly fit three lines of text without the text wrapping to a fourth line and the text width should be as narrow as possible.

I realize that if I can figure out how to do it for a textbox or a label I will have more or less solved the problem. Possibly it can be done with the Windows API, but any advice will be appreciated.

1

There are 1 answers

1
Gary Richter On

I'm not as familiar with windows development but I have a .net assembly that prints labels and in the UI a user would use to add information to the label, I need to calculate the length of the overall text against the number of lines they say they want the text to fit on and if I determine the text is to long to fit on (x) number of lines based on the text they provide and the font size they specify, I shrink the font size, recalculate, and repeat until it all fits.

Depending on the font you are using, whether it's monospaced or not, the length is going to be different (monospace will obviously have the width of each character the same, non-monospaced will be different based on the character... like the different between M and I).

In my c#.net applications I'm using the Graphics.MeasureString (textString, font[name,size,style,units]) to tell me how wide my string is.

You may have something similar available. If you can calculate the width of the text string you can divide by 3 and apply the number of columns accordingly.