|
|
Title | Detect whether text in a multiline TextBox is wrapped in Visual Basic .NET |
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 .NET, Visual Basic, VB.NET |
Categories | Controls, Strings |
|
|
The following code examines each of the lines in the TextBox. The idea is that the first and last characters in the line will have different Y coordinates if the line is wrapped.
For each line, the program uses the control's GetFirstCharIndexFromLine method to get the index of the line's first character. It then adds the line's length (minus one) to get the index of the last character.
The code then uses the TextBox's GetPositionFromCharacter method to see where the characters are.
|
|
Dim wrapped As Boolean = False
For line_num As Integer = 0 To txtStuff.Lines.Length - 1
If txtStuff.Lines(line_num).Length > 1 Then
Dim pos1 As Integer = _
txtStuff.GetFirstCharIndexFromLine(line_num)
Dim pos2 = pos1 + txtStuff.Lines(line_num).Length - _
1
Dim y1 As Integer = _
txtStuff.GetPositionFromCharIndex(pos1).Y
Dim y2 As Integer = _
txtStuff.GetPositionFromCharIndex(pos2).Y
If y1 <> y2 Then
wrapped = True
Exit For
End If
End If
Next line_num
If wrapped Then
txtStuff.BackColor = Color.Yellow
Else
txtStuff.BackColor = Color.White
End If
|
|
The main program uses this code in the TextBox's TextChanged event and in the form's Resize event.
|
|
|
|
|
|