|
|
Title | Detect whether text in a multiline TextBox is wrapped in Visual Basic 6 |
Description | This example shows how to detect whether text in a multiline TextBox is wrapped in Visual Basic .NET. |
Keywords | TextBox, word wrap, multiline, Visual Basic 6, Visual Basic |
Categories | Controls, Strings |
|
|
The CheckTextBoxWrapping subroutine splits the TextBox's text into lines. For each line, it uses the form's TextWidth method to see how wide the text is. It compares the width needed to draw the text with the TextBox's width, minus a little bit for the TextBox's internal margins and borders.
|
|
' Note this relies on the fact that the
' form and TextBox are using the same font.
Private Sub CheckTextBoxWrapping()
Const MARGIN As Single = 80
Dim lines() As String
Dim i As Integer
Dim wrapped As Boolean
Dim wid As Single
wid = txtStuff.Width - MARGIN
wrapped = False
lines = Split(txtStuff.Text, vbCrLf)
For i = LBound(lines) To UBound(lines)
If Me.TextWidth(lines(i)) > wid Then
wrapped = True
Exit For
End If
Next i
If wrapped Then
txtStuff.BackColor = vbYellow
Else
txtStuff.BackColor = vbWhite
End If
End Sub
|
|
The main program uses this code in the TextBox's Changed event and in the form's Resize event.
This method is not guaranteed to be perfectly correct, largely due to the TextBox's borders and internal margins.
|
|
|
|
|
|