Click to See Complete Forum and Search --> : text width and height?
yonatan_maman
October 11th, 2001, 10:33 AM
Im looking for a way to know what will be the width and height of text that that I want to put on a control (like label) before I put the text.
I need that the result width and height will be a function of the control size but also the font property.
any idea?
thanx Yonatan
Iouri
October 11th, 2001, 10:45 AM
I think the width and height will depend on the font. You have to figure out this things for the font you are using. Also it is possible only for non proportional fonts like Courier. If you are using proportional fonts (like Times etc) the width will depend on letters you are using in your string.
You can set label property AutoSize to true, choose a non proportional font and experiment with different strings. If AutoSize = True, the label width will automatically adjust to the width of the string. Check the width of the Label control (MsgBox Label1.Width) and find the correlation between your font and the width.
Iouri Boutchkine
iouri@hotsheet.com
DSJ
October 11th, 2001, 11:01 AM
Use the forms TextWidth and Textheight methods...
option Explicit
private Sub Command1_Click()
Dim sWidth as Single
Dim sHeight as Single
sWidth = Form1.TextWidth(Command1.Caption)
sHeight = Form1.TextHeight(Command1.Caption)
MsgBox "Width - " & sWidth & vbCrLf & "Height - " & sHeight
End Sub
John G Duffy
October 11th, 2001, 12:46 PM
The textWidth and TextHeight methods mentioned earlier only work for Forms, Pictureboxes or Printers.
If the Form Font and Fontsize are not identical to the one you choose for the label, then the results will be different.
You can however fake it out by adding a PictureBox to your form with its Visible property set to false. Set its Font and FontSize equal to the corresponding Label properties then use the textwidth and textHeight methods like so.
private Sub Command1_Click()
Dim lngTextHeight
Dim lngTextWidth
Label1.Caption = "This is the Label Caption Text" ' set the labels caption
Picture1.Font = Label1.Font ' copy the font to the picturebox
Picture1.FontSize = Label1.FontSize ' copy the fontsize
lngTextHeight = Picture1.TextHeight(Label1.Caption) ' figure the text height
lngTextWidth = Picture1.TextWidth(Label1.Caption) ' figure the text width
Label1.Width = lngTextWidth ' set labels width
Label1.Height = lngTextHeight ' set labels height
MsgBox lngTextHeight & " " & lngTextWidth ' diagnostics
End Sub
John G
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.