Emulating Overstrike Mode in Text Boxes

Windows text boxes always work in insert mode and don’t provide anoverstrike mode. However, overstrike mode can easily be emulated as shownhere:

 Sub Text1_KeyPress (KeyAscii As Integer) If KeyAscii >= 32 Then If Text1.SelLength = 0 Then If Text1.SelStart < Len(Text1) Then Text1.SelLength = 1 End If End If End If End Sub 

If a key is typed when no text is selected, the code selects thecurrent character by setting the SelLength property to 1, causing it tobe overwritten by the key typed. If the cursor is already at the end ofthe text, setting the SelLength property to 1 is silently ignored. Severalconditions are tested to ensure that a control character (such as Tab orEnter) was not pressed, that the user hasn't already highlighted text tobe replaced, and that the caret is not at the end of the existing text.

Share the Post:
Share on facebook
Share on twitter
Share on linkedin


Recent Articles: