Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleDetect whether text in a multiline TextBox is wrapped in Visual Basic 6
DescriptionThis example shows how to detect whether text in a multiline TextBox is wrapped in Visual Basic .NET.
KeywordsTextBox, word wrap, multiline, Visual Basic 6, Visual Basic
CategoriesControls, 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.

 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated