Capturing the Deleted Text in TextBox when Backspace or Delete Key is Pressed

If you have ever wanted to get the deleted character/text from a textbox when the Backspace key or the Delete key on the keyboard is pressed, you would know that there is no easy way to do that. You can easily get the typed text (using any of the keyboard events like KeyDown, KeyPress etc.), but not the deleted text.

However it is not impossible to achieve this. All you need is some event/method that executes before the KeyDown event is fired. And fortunately there are many.

For this demo I will show you how to do that using the ProcessKeyMessage , but you can use any of the others as well (e.g. WndProc etc.)

Public Class TextBoxEx
    Inherits TextBox

    Private Const WM_KEYDOWN = &H100

    '' This property stores our deleted text
    '' If you are using pre vb2010, you wll have to put the full property declaration here with Get and Set
    Public Property DeletedText As String

    Protected Overrides Function ProcessKeyMessage(ByRef m As System.Windows.Forms.Message) As Boolean
        If m.Msg = WM_KEYDOWN Then
            Dim keyCode As Keys = CType(m.WParam, Keys) And Keys.KeyCode
            Select Case keyCode
                Case Keys.Back
                    If Me.SelectionLength = 0 Then
                        If Me.SelectionStart > 0 Then DeletedText = Me.Text.Substring(Me.SelectionStart - 1, 1)
                        DeletedText = Me.SelectedText
                    End If

                Case Keys.Delete
                    If Me.SelectionLength = 0 Then
                        If Me.SelectionStart < Me.TextLength Then DeletedText = Me.Text.Substring(Me.SelectionStart, 1)
                        DeletedText = Me.SelectedText
                    End If
            End Select
        End If
        Return MyBase.ProcessKeyMessage(m)
    End Function

    Protected Overrides Sub OnKeyPress(e As System.Windows.Forms.KeyPressEventArgs)
        If e.KeyChar  vbBack Then DeletedText = ""
    End Sub

End Class

That’s all we need to do.

To test the above code, just add the class to a new Windows forms application and recompile the application.

You should get a new item named TextBoxEx in the toolbox. Drag it on to your form and set its Multiline property to True. Then add the following code:

Private Sub TextBoxEx1_TextChanged(sender As System.Object, e As System.EventArgs) Handles TextBoxEx1.TextChanged
    Debug.Print("Deleted Text is: " & TextBoxEx1.DeletedText)
End Sub

Run the application and see what is printed in the Debug window. Notice that when you delete something in the textbox, it is saved in the DeletedText property and reflected in the Debug window.

For this demo, I’m storing the immediate deleted text only. But you can modify the class easily to make it behave whatever way you like.



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: