0

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.

JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
sawme
  • 51
  • 1
  • 5
  • 1
    I believe I've fudged this before by using the form's `TextWidth` and `TextHeight` properties. – jac Sep 11 '13 at 22:51
  • Useful information in this similar question [here](http://stackoverflow.com/questions/400732/centering-fonts-in-vb6), although not addressing multi line text – MarkJ Sep 12 '13 at 07:23
  • @MarkJ I've seen solutions for one line of text, but they don't work for multiple lines, and I haven't figured out how to apply them to multiple lines. – sawme Sep 12 '13 at 18:19

1 Answers1

0

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.

Gary Richter
  • 526
  • 4
  • 16
  • 1
    The problem is that if I take the length of the string and divide by three, if there are long words on the first line, they will cause there to be more than three lines. – sawme Sep 12 '13 at 00:31