UserForm TextBox with fixed Width and dynamic Height

S

Soniya

Hello,

Can we have a multi line TextBox in a Userform with fixed width but the
height adjusting according to the length of text? Say if the textbox
contains text to be fit in five lines then adjust the height
accrdingly? also with a maximum height fixed, so that it may not be
more than the height of the userfom itself.

Thanks
 
H

Helmut Weber

Hi Soniya,

like this:

Private Sub TextBox1_Change()
TextBox1.Width = 100
' to restrict autosize to vertical
If TextBox1.Height > 75 Then
TextBox1.Height = 75
TextBox1.AutoSize = False
End If
End Sub


plus
multiline = true
autosize = true
wordwrap = true

Unfortunately, when typing some lines of text,
the first line vanishes temporarily,
but reappears when the textbox looses focus.

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Just fooling around with Excel a bit

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
B

Bob Phillips

You could play around a bit with it, by adjusting the height by the number
of characters, but I doubt that you can be totally accurate as it depends
upon where the word breaks.

Private Sub TextBox1_Change()
With Me.TextBox1
.Height = Application.Min(Me.Height - 32, (Int(Len(.Text) /
Int(.Width / 4.7)) + 1) * 18)
End With
End Sub

should get you started on the idea, but you will need to play with the
variables to suit.

--
HTH

Bob Phillips

(remove nothere from email address if mailing direct)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top