CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Oct 2001
    Posts
    2

    text width and height?

    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


  2. #2
    Join Date
    May 2000
    Location
    New York, NY, USA
    Posts
    2,878

    Re: text width and height?

    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
    [email protected]
    Iouri Boutchkine
    [email protected]

  3. #3
    Join Date
    Jun 2001
    Location
    MO, USA
    Posts
    2,868

    Re: text width and height?

    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





  4. #4
    Join Date
    Apr 2000
    Location
    South Carolina,USA
    Posts
    2,210

    Re: text width and height?

    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured