Results 1 to 7 of 7

Thread: Get the height in pixels of the text in a text box - HardCore?

  1. #1

    Thread Starter
    Hyperactive Member Trojan's Avatar
    Join Date
    Dec 2003
    Location
    Area 51
    Posts
    280

    Get the height in pixels of the text in a text box - HardCore?

    I Filled a TextBox with a text from a text file... no I want to size the height of the TextBox to the Height of the text.

    can It be done? API? Any Ideas?

    Thanks in advanced.
    MD5How To Detect Disk In Drive X?Time Tickerchanging the owner of a controlSystray HardCore

    • "Programming is like sex: one mistake and you have to support it for the rest of your life."
    • "A program is a spell cast over a computer, turning input into error messages."
    • "WARNING: Keyboard Not Attached. Press F10 to Continue."
    • "Why doesn't DOS ever say 'EXCELLENT command or filename!'"

  2. #2
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Get the height in pixels of the text in a text box - HardCore?

    Make sure the forms font is set the same as the textbox and do:

    Code:
    MsgBox Form1.TextHeight(Text1.Text)

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  3. #3
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: Get the height in pixels of the text in a text box - HardCore?

    TextHeight can be used if the string isn't a multiline string. If it is you can still use TextHeight * NumberOfLines. But if the textbox is wordwrapping it might be hard to know the exact number of lines. In that case you can use the DrawText API. Which also could be used to for example make a label autosize vertically.
    VB Code:
    1. Private Declare Function DrawText _
    2.  Lib "user32.dll" Alias "DrawTextA" ( _
    3.  ByVal hdc As Long, _
    4.  ByVal lpStr As String, _
    5.  ByVal nCount As Long, _
    6.  ByRef lpRect As RECT, _
    7.  ByVal wFormat As Long) As Long
    8.  
    9. Private Const DT_CALCRECT As Long = &H400
    10.  
    11. Private Type RECT
    12.     Left As Long
    13.     Top As Long
    14.     Right As Long
    15.     Bottom As Long
    16. End Type
    17.  
    18. Private Sub AdjustHeight(txtBox As TextBox)
    19.     Dim sText As String
    20.     Dim r As RECT
    21.     Dim nHeight As Long
    22.    
    23.     'adjust the scale mode for easier calculations
    24.     Me.ScaleMode = vbPixels
    25.     r.Right = txtBox.Width - 4 ' -4 px for the border, assumes 3d style is used
    26.     sText = txtBox.Text
    27.     nHeight = DrawText(Me.hdc, sText, Len(sText), r, DT_CALCRECT)
    28.     If nHeight Then
    29.         txtBox.Height = nHeight + 6
    30.     End If
    31. End Sub

  4. #4
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Get the height in pixels of the text in a text box - HardCore?

    This function will wrap the text and use .TEXTHEIGHT.

    Code:
    frmTeaMark.TextHeight(Wrap_Text(txtData.Text, txtData.Width))
    Code:
    Private Function Wrap_Text(ByVal strText As String, lngWidth As Long) As String
    
    Dim x As Long, y As Long, z As Long, s1 As String
    
    s1 = ""
    
    Do While strText <> ""
        For x = 1 To Len(strText)
            Select Case Asc(Mid$(strText, x, 1))
                Case 32
                    y = x
                Case 13
                    Debug.Print "Got 13"
                Case 10
                    Debug.Print "Got 10"
                    z = x
            End Select
            If z <> 0 Then
                Debug.Print Left(strText, z - 2)
                If s1 <> "" Then s1 = s1 & vbCrLf
                s1 = s1 & Left(strText, z - 2)
                strText = Mid(strText, z + 1)
                z = 0
                Exit For
            End If
            If frmTeaMark.TextWidth(Left(strText, x)) > lngWidth Then
                Debug.Print Left(strText, y - 1)
                If s1 <> "" Then s1 = s1 & vbCrLf
                s1 = s1 & Left(strText, y - 1)
                strText = Mid(strText, y + 1)
                Exit For
            End If
            If x = Len(strText) Then
                Debug.Print strText
                If s1 <> "" Then s1 = s1 & vbCrLf
                s1 = s1 & strText
                strText = ""
                Exit For
            End If
        Next x
    Loop
    
    Wrap_Text = s1
    
    End Function

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  5. #5
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: Get the height in pixels of the text in a text box - HardCore?

    Quote Originally Posted by szlamany
    This function will wrap the text and use .TEXTHEIGHT.
    All that is fine but a textbox automatically wraps text if you don't show the horizontal scrollbar and in that case the DrawText is the way to go since it does the calculation for you. Less code and faster result.

  6. #6
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Get the height in pixels of the text in a text box - HardCore?

    Quote Originally Posted by Joacim Andersson
    All that is fine but a textbox automatically wraps text if you don't show the horizontal scrollbar and in that case the DrawText is the way to go since it does the calculation for you. Less code and faster result.
    We end up having to store the data in a SQL table - and also having to print it - so wrapping the text and inserting those "assumed" cr/lf's was necessary.

    Unfortunately you cannot stop a textbox at a certain height - so when the user leaves the textbox we needed to make sure they didn't put too much in the textbox to fit on the report card...

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  7. #7
    Hyperactive Member notquitehere188's Avatar
    Join Date
    Dec 2004
    Posts
    403

    Re: Get the height in pixels of the text in a text box - HardCore?

    if you dont want to do those other ideas,(they scare me) lol
    you can use currentx and currenty to calculate it
    (currentx and currenty are the co-ordinates of the next print statement on a form/picturebox)
    Code:
    string = textxox.text
    
    for x = 1 to len(string)
    if currenty > (maximum width you want it to go to) then
    print mid(string,x,1)
    else 
    print mid(string, x,1);
    end if
    next x
    
    textbox .height = currentx + (a bit so its not cramped)
    the only problem with that is it does not correct for words staying together(if it even matters)
    just make sure the form and textbox text settings are the same and you might have to change it slightly to give more extra
    It's not just Good, It's Good enough!



    Spelling Eludes Me

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