English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

I have a text box in a program I'm writing that I need to become bigger so a user who's inputting text can see what they are doing.
Stats on the text box:
With Text1
.Text=""
.Scrollbars=2 'vertical
.Multiline=True
End With

Once the text reaches the end and wraps I want the Text1.Height to go up for visibility. How can I do this?

If ???? Then
Text1.Height = 300
End If

Later in Text1_LostFocus I'll change the height back...

But what would be the conditions for the program to realize that the text has wrapped?

PS I don't want to do string manipulation or font changing.. Thank you for your time!

2007-10-02 12:32:43 · 3 answers · asked by neofrog46307 2 in Computers & Internet Programming & Design

3 answers

Copy and past the following code into a form which has a textbox named text1.

This code is based upon a knowledge base article Q140886 dealing with printing multi line text boxes. I just stripped out the section which counts the lines. The code make use of the Windows API to determine the number of lines in a multi line text box. It obtains handle info for a textbox named "text1"
So you will have to modify the code if you have a different name or want to deal with more than one textbox.

I have added code to three events goFocus, LostFocus and TextChanged to handle changing the size.


#If Win32 Then
Private Declare Function SendMessageAsLong Lib "user32" _
Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, _
ByVal wParam As Long, ByVal lParam As Long) As Long
Private Declare Function SendMessageAsString Lib "user32" _
Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, _
ByVal wParam As Long, ByVal lParam As String) As Long
Dim Buffer As String
Dim resizing As Integer
Const EM_GETLINE = 196
Const EM_GETLINECOUNT = 186
#Else
Private Declare Function SendMessage% Lib "user" _
(ByVal hWnd%, ByVal wMsg%, ByVal wParam%, ByVal lParam As Any)
Dim Buffer As String
Dim resizing As Integer
Const EM_GETLINE = &H400 + 20
Const EM_GETLINECOUNT = &H400 + 10
#End If
Const MAX_CHAR_PER_LINE = 80 ' Scale this to size of text box.



Private Function fGetLineCount() As Long
' This function will return the number of lines
' currently in the Text1 text-box control.

#If Win32 Then
lcount = SendMessageAsLong(Text1.hWnd, EM_GETLINECOUNT, 0, 0)
#Else
lcount = SendMessage(Text1.hWnd, EM_GETLINECOUNT, 0&, 0&)
#End If


fGetLineCount = lcount

End Function


Private Sub Text1_Change()
'Update text height as text is typed or removed
If fGetLineCount > 1 Then
Me.Text1.Height = 1000
Else
Me.Text1.Height = 300
End If

End Sub

Private Sub Text1_GotFocus()
If fGetLineCount > 1 Then
Me.Text1.Height = 1000
Else
Me.Text1.Height = 300
End If
End Sub

Private Sub Text1_LostFocus()
'Restore textbox height to minimum size
Me.Text1.Height = 300
End Sub

2007-10-02 14:20:04 · answer #1 · answered by MarkG 7 · 0 0

If Text1.MultiLine = True Then
Me.Font = Text1.Font
Dim lines As Integer
lines = (Me.TextWidth(Text1.Text) - 1400) / Text1.Width + 1
Me.Caption = Me.TextWidth(Text1.Text) & ", " & Text1.Width
Text1.Height = Me.TextHeight(Text1.Text) * (lines + 1)
End If


most likely.. that is the idea.. you just need to improve it though.. use the TextWidth and TextHeight function of a PictureBox or simply the Form itself... But you need to set their Font as the the same... Then... do mathematics! (^^,)

2007-10-02 14:02:52 · answer #2 · answered by Anonymous · 0 0

I don't have VB installed at home atm so I can't check but if it were a mono-spaced font (courier type ones) you could keep a check on the number of characters typed and if it exceeds your width (in characters) change the height. If it's a kerned font (like arial or times) where each character has a different width, you could do some educated guesses as to the number of characters it needs to change the height.

Sorry if that doesn't suit - like I said - I can't play with it atm to check.

2007-10-02 12:45:49 · answer #3 · answered by Steve B 3 · 0 0

fedest.com, questions and answers