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.
Printable View
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.
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.
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