Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

I'm trying to figure out how many lines will display in a multiline textbox control. I've attempted to calculate it by using the GetHeight method of the textbox's Font object divided into the Height property of the textbox's ClientRectangle property. When I size the textbox to over 20 lines, the calculation doesn't work. It seems that the height actually used for each line in the textbox is a little more than what the font's GetHeight returns. I've also tried simply using the Height property of the font in the calculation. No luck there, either.

 

' My orginal code is like this:

Dim cFont As Font = txtBox.Font

Dim lineHeight As Single = cFont.GetHeight(txtBox.CreateGraphics())

Dim boxHeight As Single = txtBox.ClientRectangle.Height

Dim maxLinesVisible As Decimal = boxHeight / lineHeight

 

result is Decimal.Floor(maxLinesVisible)

 

Is there some sort of metric that measures the height actually used for some spacing between the lines or something else???? :confused:

  • *Experts*
Posted

Try MeasureString (part of the Graphics object), which returns a SizeF object with the width and height.

 

-Nerseus

"I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
Posted

You nailed it dead on:

 

Dim g As Graphics = txtBox.CreateGraphics()

Dim sf As SizeF = g.MeasureString(sLine, cFont, txtBox.Width)

 

Then use sf.Height in the calculation.

 

Thanks 1,000,000!:)

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...