devxlogo

OverwriteHandler – A class for handling overwrite mode

OverwriteHandler – A class for handling overwrite mode

'---------------------------------------------------------' OverwriteHandler class'' you can associate this class to a form, using the' Form property, and all the textbox controls in the' form will support insert/overwrite mode switching'' Usage:  Dim ov As New OverwriteHandler''         Private Sub Form_Load()'            Set ov.Form = Me'            ' if you want block-shaped caret in ov mode'            ov.BlockCaret = True'         End Sub'' The class responds as expected to the INS key to' switch among insert and overwrite mode. You can do' the same by acting over the OverwriteMode property'' The OverwriteMode property is automatically reset to' false whenever the focus enters a new field, but is' preserved when the focus shifts to another form and back'' The caret shape is not correctly preserved when the input' focus shifts to another application and then back, but the' overwrite mode is.'' Notes: this class forces the form's KeyPreview property to True''        There are a few controls (most notably, the ComboBox'        control) for which the block caret doesn't display correctly.'        Other controls manage the caret in a way that this class'        can display a custom caret. In such cases you should leave'        the ShowBlock property to False.''---------------------------------------------------------Option ExplicitPrivate Declare Function CreateCaret Lib "user32" (ByVal hWnd As Long, _    ByVal hBitmap As Long, ByVal nWidth As Long, ByVal nHeight As Long) As LongPrivate Declare Function ShowCaret Lib "user32" (ByVal hWnd As Long) As Long' member variable for OverwriteMode propertyPrivate m_OverwriteMode As Boolean' member variable for Form propertyPrivate WithEvents HookedForm As Form' this keeps track of the last active fieldPrivate ActiveControl As Control' member variable for BlockCaret propertyPrivate m_BlockCaret As Boolean' The Hooked FormPublic Property Get Form() As Form    Set Form = HookedFormEnd PropertyPublic Property Set Form(ByVal newValue As Form)    Set HookedForm = newValue    ' enforce KeyPreview = True, so that this class    ' can trap keys pressed in the form    If Not (HookedForm Is Nothing) Then        HookedForm.KeyPreview = True    End IfEnd Property' The current Overwrite flagPublic Property Get OverwriteMode() As Boolean    CheckActiveControl    OverwriteMode = m_OverwriteModeEnd PropertyPublic Property Let OverwriteMode(ByVal newValue As Boolean)    m_OverwriteMode = newValue    UpdateCaretEnd Property' True if the overwrite caret should be displayed as a blockProperty Get BlockCaret() As Boolean    BlockCaret = m_BlockCaretEnd PropertyProperty Let BlockCaret(ByVal newValue As Boolean)    m_BlockCaret = newValue    UpdateCaretEnd Property' rebuild the caret block' at times you may need to call this method directly, for example' when the focus shift to another application and then back.Public Sub UpdateCaret()    On Error Resume Next    If Not CheckActiveControl Then        ' active control doesn't support carets    ElseIf OverwriteMode And BlockCaret Then        ' show the caret as a block        CreateCaret ActiveControl.hWnd, 0, 6, 14        ShowCaret ActiveControl.hWnd    Else        ' show the caret as a vertical line        CreateCaret ActiveControl.hWnd, 0, 1, 14        ShowCaret ActiveControl.hWnd    End IfEnd Sub' reset the OverwriteMode property if the focus' has moved to another field' the function returns True if the active control' supports a caretPrivate Function CheckActiveControl() As Boolean    If Not Form.ActiveControl Is ActiveControl Then        Set ActiveControl = Form.ActiveControl        OverwriteMode = False    End If    On Error Resume Next    ' the following expression is always True, unless the    ' active control doesn't refer to a textbox    CheckActiveControl = (ActiveControl.SelLength >= 0)End Function' trap user's key pressesPrivate Sub HookedForm_KeyPress(KeyAscii As Integer)    On Error Resume Next        If Not CheckActiveControl Then        ' active control is not of type textbox        ' do nothing    ElseIf m_OverwriteMode And KeyAscii >= 32 And ActiveControl.SelLength = 0 _        Then        ' we are in overwrite mode, the user hasn't pressed a        ' control key and there's no text currently highlighted        If Mid$(ActiveControl.Text, ActiveControl.SelStart + 1, 1) <> vbCr Then            ' extend the selection if not at the end of the line            ActiveControl.SelLength = 1        End If    End IfEnd Sub' check if INS key has been pressedPrivate Sub HookedForm_KeyDown(KeyCode As Integer, Shift As Integer)    If CheckActiveControl Then        If KeyCode = 45 And Shift = 0 Then            ' Insert key has been pressed            OverwriteMode = Not OverwriteMode        End If    End IfEnd Sub' rebuild the correct caret when the forms regains the focusPrivate Sub HookedForm_Activate()    If CheckActiveControl Then UpdateCaretEnd Sub

See also  Why ChatGPT Is So Important Today
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist