Results 1 to 3 of 3

Thread: How to resize a TextBox?

  1. #1

    Thread Starter
    Addicted Member Michel Jr's Avatar
    Join Date
    Jan 2000
    Location
    Brazil
    Posts
    175

    Question

    Hi,


    I'm in trouble trying to resize a TextBox.
    Is it possible to get the size of the Font and then resize the TextBox (increasing or decreasing), avoiding the text to be hidden by the borders?


    Thanks for any help...

    Michel Jr.

  2. #2
    Fanatic Member
    Join Date
    Mar 2000
    Location
    That posh bit of England known as Buckinghamshire
    Posts
    658
    1. You will need to set the Forms font to the exact same as the text box's. Including Bold, Italics etc.

    2. Use the TextWidth function to find out how long the text will be, and the TextHeight function to return the Height.

    3. Resize the TextBox to the values returned from the above function.
    Iain, thats with an i by the way!

  3. #3
    Frenzied Member
    Join Date
    Mar 2000
    Posts
    1,089
    Damn beaten to it, here's some code anyway.

    I'm using the gettextextentpoint API because I was trying to do it without transferring it to the form, but it's the same principal

    Code:
    Option Explicit
    
    Private Type Size
            cx As Long
            cy As Long
    End Type
    
    Private Declare Function GetTextExtentPoint Lib "gdi32" Alias "GetTextExtentPointA" (ByVal hdc As Long, ByVal lpszString As String, ByVal cbString As Long, lpSize As Size) As Long
    
    Private Sub Command1_Click()
    Dim apiTextSize As Size
    Dim strTemp As String
    
    'We need a temporary string as we're passing to an API
    strTemp = Text1.Text
    
    
    'Now we change the font of our form to the font of the TextBox
    'NB normally you'd use an stdFont object to store the old font in
    'and replace afterwards, but I'm lazy
    Set Me.Font = Text1.Font
    
    'Find out the size of the text
    Call GetTextExtentPoint(Me.hdc, strTemp, Len(strTemp), apiTextSize)
    
    'Set the Width and the height of the textbox, the +8 and +4 adjustments allow for the border
    Text1.Width = Me.ScaleX(apiTextSize.cx + 8, vbPixels, Me.ScaleMode)
    Text1.Height = Me.ScaleY(apiTextSize.cy + 4, vbPixels, Me.ScaleMode)
    
    End Sub

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