Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

textwidth & textheight

Status
Not open for further replies.

deulyd

Programmer
Oct 17, 2001
106
CA
Hello,
I am trying to create a textbox that resizes as the user type data in it to fit the text. I am currently using the textwidth and textheight methods but they seems working weirdly. Those methods return me an appropriate value but it's too small to fit the text in it.
Here is my code

Thanks



Private Sub Text1_Change()

Dim sinFontSize As Single
Dim strFontName As String
Dim str As String
Dim intLen As Integer
Dim intHei As Integer

str = Me.Text1

' stock the original form values
sinFontSize = Me.FontSize
strFontName = Me.FontName

' set the form values to those of the textbox
Me.FontSize = Me.Text1.FontSize
Me.FontName = Me.Text1.FontName

' calculate the lenght and height of the textbox
intLen = Picture1.TextWidth(str)
intHei = Picture1.TextHeight(str)

' resize the textbox
Me.Text1.Width = intLen
Me.Text1.Height = intHei

' set the form to it's original values
Me.FontSize = sinFontSize
Me.FontName = strFontName


End Sub

 
sorry, you should read :

' calculate the lenght and height of the textbox
intLen = me.TextWidth(str)
intHei = me.tHeight(str)

 
sorry, you should read :

' calculate the lenght and height of the textbox
intLen = me.TextWidth(str)
intHei = me.textHeight(str)

 
The following is an example of what I've done in my programs:

Create a form with one textbox named "Text1".

Option Explicit

Dim intKey As Integer

Private Sub Text1_Change()

Dim intLength As Integer
'Set Textbox width to 250 and alignment to center

Me.Font = Text1.Font
Me.FontSize = Text1.FontSize

intLength = Me.TextWidth(Text1)

Text1.Width = intLength + 270


End Sub

Private Sub Text1_KeyPress(KeyAscii As Integer)

intKey = KeyAscii

End Sub

Private Sub Text1_KeyUp(KeyCode As Integer, Shift As Integer)

If intKey = 22 Then Text1.SelStart = Len(Text1)

End Sub


 
There's some spacing around the string that the basic width and height calculations don't seem to take into account. There is an API method of dealing with the, but I find it more hassle than it's worth. The general way I deal with it is to temporarily add a couple of spaces onto the string when using the TextWidth methods, which seems to do the trick.
 
I use a label with WordWrap=false,AutoSize=True, BackStyle=Transparent, set to Font characteristics as the textbox. The label must be visible when Left etc are accessed
Can't Hurt.
Code:
Sub txt_Change()
    with lblSizeit
        .Forecolor = me.backcolor ' Hide caption
        .Caption = txt.text
        .Visible = True
        txt.move txt.left,txt.top, .width + ?, .height + ?
        .Visible = false
        .Caption = ""
    End With
End Sub
[/code
      [URL unfurl="true"]www.VBCompare.com[/URL] Compare Code (Text)
[URL unfurl="true"]www.VBSortGen.com[/URL] Generate Sort in VB or VBScript
 

Thanks everyone I found my answer in the code of nebelmann my problem was that I was changing the FontSize before the FontName. It's weird but it's working

thanks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top