The information in this article applies to:
- Standard and Professional Editions of Microsoft Visual Basic for
Windows, versions 2.0 and 3.0
SUMMARY
Although Visual Basic text boxes do not support an overtype (replace) mode
(where the text you type replaces the text already there) you can write
code to add this support. The example below demonstrates two methods for
implementing overtype mode in a Visual Basic text box.
MORE INFORMATION
Microsoft Visual Basic for Windows text-box controls default to insert
mode, inserting the text you type rather than replacing what is already
there (overtype mode). To emulate overtype mode, you can add code to the
KeyPress event for a text box. The following code fragment assumes you have
a text box named Text1 that you want to have in overtype mode:
' Form level code:
Const KEY_BACK = &H8
Sub Text1_KeyPress (KeyAscii As Integer)
If KeyAscii <> KEY_BACK Then
Text1.SelLength = 1
End If
End Sub
By setting the SelLength property of the text box to 1 you force the text
box to highlight (select) the next character after the current caret
position. When the KeyPress event completes and the corresponding character
is added to the text in the text box, it automatically replaces the
highlighted character.
If you want to allow the user to dynamically switch between insert and
overtype mode, you can add additional code to support this; you can also
use the SelLength property to show the user which character will be
replaced when he or she is in overtype mode. The following sample program
demonstrates this:
Steps to Create Sample Program
- Run Visual Basic; or if Visual Basic is already running, choose New
Project (ALT+F, N) from the File menu. Form1 is created by default.
- Add one text box to Form1 named Text1.
- Add one label box to Form1 named Label1.
- Enter the following code in the Form1 General section:
' Key codes:
Const KEY_BACK = &H8
Const KEY_PRIOR = &H21
Const KEY_NEXT = &H22
Const KEY_END = &H23
Const KEY_HOME = &H24
Const KEY_LEFT = &H25
Const KEY_UP = &H26
Const KEY_RIGHT = &H27
Const KEY_DOWN = &H28
Const KEY_INSERT = &H2D
Const MODE_OVERTYPE = "overtype"
Const MODE_INSERT = "insert"
- Enter the following code in the Form1 Load procedure:
Sub Form_Load ()
Text1.Tag = MODE_INSERT
Label1.Caption = MODE_INSERT
End Sub
- Enter the following code in the Text1 Change procedure:
Sub Text1_Change ()
' You have taken some action that changed the text in the
' text box. Reset the SelLength if you are in overtype mode.
If Text1.Tag = MODE_OVERTYPE And Text1.SelLength = 0 Then
Text1.SelLength = 1
End If
End Sub
- Enter the following code in the Text1 KeyPress procedure:
Sub Text1_KeyPress (KeyAscii As Integer)
' If you press BACKSPACE and are in overtype mode,
' then set SelLength to 0 so the backspace will correctly
' delete the character to the left of the current caret
' position. SelLength will be reset when the Text1_Change
' event occurs following the backspace.
If KeyAscii = KEY_BACK And Text1.Tag = MODE_OVERTYPE Then
Text1.SelLength = 0
End If
End Sub
- Enter the following code in the Text1 KeyUp procedure:
Sub Text1_KeyUp (KeyCode As Integer, Shift As Integer)
Select Case KeyCode
' Toggle between insert and overtype modes.
Case KEY_INSERT
If Text1.Tag = MODE_OVERTYPE Then
Text1.Tag = MODE_INSERT
Label1.Caption = MODE_INSERT
Else
Text1.SelLength = 1
Text1.Tag = MODE_OVERTYPE
Label1.Caption = MODE_OVERTYPE
End If
' Handle keys that move the caret position and reset the
' SelLength if you are in overtype mode:
'
' The following two lines of code should be all on one line:
Case KEY_LEFT, KEY_RIGHT, KEY_UP, KEY_DOWN, KEY_HOME, KEY_END,
KEY_PRIOR, KEY_NEXT
If Text1.Tag = MODE_OVERTYPE Then
Text1.SelLength = 1
End If
End Select
End Sub
- Enter the following code in the Text1 MouseUp procedure. The Sub
statement should all be on one line of code:
Sub Text1_MouseUp (Button As Integer, Shift As Integer, x As Single,
Y As Single)
' You have clicked at a new location within the text box. Reset the
' SelLength if you are in overtype mode.
If Text1.Tag = MODE_OVERTYPE And Text1.SelLength = 0 Then
Text1.SelLength = 1
End If
End Sub
- Press the F5 key to run the program. When you press INSERT, the Label1
label shows the current mode of the Text1 text box. While in overtype
mode, the current character to be replaced is highlighted.