TextBox Control for Visual Basic 6.0 Users
The TextBox control in Visual Basic 6.0 is replaced by the Windows Forms TextBox control in Visual Basic 2008. The names of some properties, methods, events, and constants are different, and in some cases there are differences in behavior.
Conceptual Differences
In Visual Basic 6.0, the MaxLength property determines the number of characters that can be entered into a TextBox control. If you attempt to insert text programmatically, the text will be truncated at the length specified by the MaxLength property.
In Visual Basic 2008, the behavior of the MaxLength property no longer applies to text that is added programmatically. To duplicate the Visual Basic 6.0 behavior, you need to truncate the string yourself.
In Visual Basic 6.0, the PasswordChar property is of type String; in Visual Basic 2008 it is of type Char.
In addition, there are numerous conceptual differences that apply to all controls, including differences in data binding, font handling, drag-and-drop operations, Help support and more. For more information, see Windows Forms Concepts for Visual Basic 6.0 Users.
Code Changes for the TextBox Control
The following examples illustrate differences in coding techniques between Visual Basic 6.0 and Visual Basic 2008.
Code Changes for Searching in a TextBox Control
The following code demonstrates how to search for a string in a TextBox control and highlight it.
' Visual Basic 6.0
Private Sub Form_Load ()
Text1.Text = "Two of the peak human experiences"
Text1.Text = Text1.Text & " are good food and classical music."
End Sub
Private Sub Form_Click ()
Dim Search, Where
' Get search string from user.
Search = InputBox("Enter text to be found:")
' Find string in text.
Where = InStr(Text1.Text, Search)
If Where Then
Text1.SetFocus
Text1.SelStart = Where - 1
Text1.SelLength = Len(Search)
Else
MsgBox "String not found."
End If
End Sub
' Visual BasicPrivateSub Form1_Load(ByVal sender As System.Object, ByVal e As _
System.EventArgs) HandlesMyBase.Load
TextBox1.Text = "Two of the peak human experiences are "
TextBox1.Text = TextBox1.Text & "good food and classical music."EndSubPrivateSub Form1_Click(ByVal sender AsObject, ByVal e As _
System.EventArgs) HandlesMe.Click
Dim Search AsStringDimWhereAsString ' Get search string from user.
Search = InputBox("Enter text to be found:")
' Find string in text.Where = InStr(TextBox1.Text, Search)
IfWhereThen
TextBox1.Focus()
TextBox1.SelectionStart = Where - 1
TextBox1.SelectionLength = Len(Search)
Else
MsgBox("String not found.")
EndIfEndSub
Code Changes for Limiting the Number of Characters in a TextBox Control
The following code demonstrates how to use the MaxLength property to specify a maximum number of characters.
' Visual Basic 6.0
Private Sub Form_Load()
Text1.MaxLength = 5
End Sub
Private Sub SetText()
' Only the first five characters will be displayed.
Text1.Text = "Hello World"
End Sub
' Visual BasicPrivateSub Form1_Load2(ByVal sender As System.Object, ByVal e As _
System.EventArgs) HandlesMyBase.Load
TextBox1.MaxLength = 5
EndSubPrivateSub SetText()
' Truncate the string to equal MaxLength.
TextBox1.Text = Strings.Left("Hello World", TextBox1.MaxLength)
EndSub
TextBox Control Property, Method, and Event Equivalencies
The following tables list Visual Basic 6.0 properties, methods, and events, along with their Visual Basic 2008 equivalents. Those properties, methods, and events that have the same name and behavior are not listed. All Visual Basic 2008 enumerations map to the System.Windows.Forms namespace unless otherwise noted.
This table provides links to topics explaining behavior differences. Where there is no direct equivalent in Visual Basic 2008, links are provided to topics that present alternatives.
Properties
Visual Basic 6.0 |
Visual Basic 2008 Equivalent |
---|---|
Alignment |
|
Appearance |
New implementation. For more information, see Appearance and BorderStyle Properties for Visual Basic 6.0 Users. |
BackColor |
Note:
Colors are handled differently in Visual Basic 2008. For more information, see Color Handling for Visual Basic 6.0 Users.
|
Container |
|
DataChanged DataField DataFormat DataMember DataSource |
New implementation. For more information, see Data Access for Visual Basic 6.0 Users. |
DragIcon DragMode |
New implementation. For more information, see Drag and Drop for Visual Basic 6.0 Users. |
Font FontBold FontItalic FontName FontSize FontStrikethrough FontUnderline |
Note:
Fonts are handled differently in Visual Basic 2008. For more information, see Font Object for Visual Basic 6.0 Users.
|
ForeColor |
Note:
Colors are handled differently in Visual Basic 2008. For more information, see Color Handling for Visual Basic 6.0 Users.
|
Height |
Note:
Coordinates are handled differently in Visual Basic 2008. For more information, see Coordinate System for Visual Basic 6.0 Users.
|
HelpContextID |
New implementation. For more information, see Help Support for Visual Basic 6.0 Users. |
HWnd |
|
Index |
New implementation. For more information, see Control Arrays for Visual Basic 6.0 Users. |
Left |
Note:
Coordinates are handled differently in Visual Basic 2008. For more information, see Coordinate System for Visual Basic 6.0 Users.
|
LinkItem LinkMode LinkTimeOut LinkTopic |
New implementation. For more information, see Dynamic Data Exchange for Visual Basic 6.0 Users. |
Locked |
|
MouseIcon |
New implementation. For more information, see Cannot set a custom MousePointer. |
MousePointer |
For a list of constants, see MousePointer for Visual Basic 6.0 Users. |
OLEDragMode OLEDropMode |
New implementation. For more information, see Drag and Drop for Visual Basic 6.0 Users. |
Parent |
FindForm method. |
SelLength |
|
SelStart |
|
SelText |
|
ToolTipText |
ToolTip component. For more information, see ToolTip Support for Visual Basic 6.0 Users. |
Top |
Note:
Coordinates are handled differently in Visual Basic 2008. For more information, see Coordinate System for Visual Basic 6.0 Users.
|
WhatsThisHelpID |
New implementation. For more information, see Help Support for Visual Basic 6.0 Users. |
Width |
Note:
Coordinates are handled differently in Visual Basic 2008. For more information, see Coordinate System for Visual Basic 6.0 Users.
|
Methods
Visual Basic 6.0 |
Visual Basic 2008 Equivalent |
---|---|
Drag |
New implementation. For more information, see Drag and Drop for Visual Basic 6.0 Users. |
LinkExecute LinkPoke LinkRequest LinkSend |
New implementation. For more information, see Dynamic Data Exchange for Visual Basic 6.0 Users. |
Move |
Note:
Coordinates are handled differently in Visual Basic 2008. For more information, see Coordinate System for Visual Basic 6.0 Users.
|
OLEDrag |
New implementation. For more information, see Drag and Drop for Visual Basic 6.0 Users. |
SetFocus |
|
ShowWhatsThis |
New implementation. For more information, see Help Support for Visual Basic 6.0 Users. |
ZOrder |
BringToFront or SendToBack function |
Events
Visual Basic 6.0 |
Visual Basic 2008 Equivalent |
---|---|
Change |
|
DblClick |
|
DragDrop DragOver |
New implementation. For more information, see Drag and Drop for Visual Basic 6.0 Users. |
GotFocus |
|
LinkClose LinkError LinkNotify LinkOpen |
New implementation. For more information, see Dynamic Data Exchange for Visual Basic 6.0 Users. |
LostFocus |
|
OLECompleteDrag OLEDragDrop OLEDragOver OLEGiveFeedback OLESetData OLEStartDrag |
New implementation. For more information, see Drag and Drop for Visual Basic 6.0 Users. |
Validate |
Upgrade Notes
When a Visual Basic 6.0 application is upgraded to Visual Basic 2008, any TextBox controls are upgraded to Windows Forms TextBox controls, and properties, methods and events are upgraded to their equivalents. Where there may be differences in behavior, upgrade comments are inserted into the code.