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
 
 
 
 
 
TitleRestrict a TextBox to at most four lines using the Change event
KeywordsTextBox, restrict, lines, carriage return
CategoriesControls
 
In the Change event handler, examine the TextBox's new value. If it is valid, save the new value in a static variable. If the value is invalid, restore the previously saved value.
 
Private Sub Text1_Change()
Static old_value As String

    ' See how many carriage returns the value has.
    ' Note that Split returns a zero-based array
    ' so UBound gives 1 less than the number of items.
    ' Restore the old value.
    If UBound(Split(Text1.Text, vbCr)) > 2 Then
        ' More than 3 carriage returns so
        ' more than 4 lines.
        Text1.Text = old_value
    Else
        ' No more than 3 carriage returns so
        ' no more than 4 lines. Save the new value.
        old_value = Text1.Text
    End If
End Sub
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated