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!

Wordwrapping a Text Box 1

Status
Not open for further replies.

kingkeith

Programmer
May 8, 2002
18
GB
Hi. Can anyone tell me how to make a text box wordwrap like Notepad does when you enable it? Is there a way to do it? Or do I need an RTF box? If there isn't a way with a normal textbox, can anyone give me some code that works out whether the line length is longer than the width, and if so, add a vbCrLf at the appropiate place?

Thanks in advance Keith
sk_kdhall@hotmail.com
 
First think you could do is set the MultiLine property of the textbox to true.

If you need more control, then you can use the Form's TextHeight and TextWidth properties to guide you in the splitting of the string. You'll need to make sure that the Font settings for the form match those for the textbox.
Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
Below is a routine which accepts two parameters, a string, and the text box in which to display that string. It relies on the underlying assumption that the font settings for the form are the same as those of the text box. If you need to change the form settings, be sure to set them back. Since Multiline is a read-only property, be sure to set it to true at design time.

One other note - if this string is going back into a database, then I would suggest that you replace the vbCrLf with a space using the Replace function before writing back to the DB.

TheString = Replace(txtTextbox.Text, vbCrlf, " ")

'=============================================================
Private Sub LoadTextBox(rStr_TheString As String, txtTheTextBox As TextBox)

Dim lStr_ThisLine As String
Dim lStr_FullLine As String
Dim lStr_Words() As String
Dim lInt_Idx As Integer
Dim lInt_MaxWidth As Integer
Dim lStr_ThisWord As String

lInt_MaxWidth = txtProject.Width - 30 ' All for a Margin
lStr_Words = Split(rStr_TheString, " ") ' Break into Words
lStr_FullLine = lStr_Words(0) ' Assign First Word
lStr_ThisLine = lStr_Words(0) ' Assign First Word
For lInt_Idx = 1 To UBound(lStr_Words) ' For the Rest of the Words
lStr_ThisWord = " " & lStr_Words(lInt_Idx) ' Add the separating space
If (Me.TextWidth(lStr_ThisLine & lStr_ThisWord) < lInt_MaxWidth) Then ' Will it Fit?
lStr_ThisLine = lStr_ThisLine & lStr_ThisWord ' Add to this line
lStr_FullLine = lStr_FullLine & lStr_ThisWord ' Add to full string
Else
lStr_FullLine = lStr_FullLine & vbCrLf & lStr_Words(lInt_Idx) ' Add CRLF and Word
lStr_ThisLine = lStr_Words(lInt_Idx) ' Start New Line with just this word
End If
Next lInt_Idx
txtTheTextBox.Text = lStr_FullLine ' Assign to text box

End Sub
Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top