MaxLength Property for Visual Basic 6.0 Users
In Visual Basic 6.0, the MaxLength property of a TextBox control has absolute control over the length of a string that can be entered or displayed in the text box. Although you can programmatically enter a longer string, it is automatically truncated to the length set in the MaxLength property.
In Visual Basic 2008, the length of a string entered programmatically overrides the MaxLength property setting.
The following code illustrates the different behavior.
' Visual Basic 6.0
Text1.MaxLength = 5
Text1.Text = "Longer than five"
MsgBox Text1.Text ' Displays "Longe".
' Visual Basic
Text1.MaxLength = 5
Text1.Text = "Longer than five"
MsgBox(Text1.Text) ' Displays "Longer than five".
What to do next
Review the design of your application to determine if the intended behavior was to always limit the text in the text box to the length set in the MaxLength property.
If this was the intended behavior, find each occurrence of code that sets the Text property, and add code to truncate the string, similar to the following example.
' Old code Text1.Text = SomeString ' Replacement codeIf Len(SomeString) > Text1.MaxLength Then SomeString = Microsoft.VisualBasic.Left(SomeString, Text1.MaxLength) EndIf Text1.Text = SomeString
If it was not the intended behavior, leave the code alone. The user will still be limited by the MaxLength setting, but you can programmatically assign strings that are longer than the MaxLength setting.