[Webinar] Streamline your web hosting managementRegister Today

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 5581
  • Last Modified:

How to capture and override Ctrl V (Paste) command in VB.Net

I have a VB.Net application and i need some way of capturing and overriding the Ctrl-V (Paste) command so that i can determine what occurs on those key events. I want to my own custom procedure to run on Ctrl-V, not paste.

In my code, i have first captured when the Control key is pressed, then if the V key is pressed. If these are both true, i then run my custom command. I also try to clear the clipboard (because a user would have put something in the clipboard to begin with if they tried to paste something), but what seems to happen is it runs my custom command, tried to empty the clipboard, but then continues to Paste the 'empty' data into the field on my form, thus clearing the field completely! The line Clipboard.SetDataObject(New DataObject) doesnt seem to actually clear the clipboard.

Does anyone know:
1) How i can completely override the Ctrl-V command in my application, so it doesnt paste and it only runs my defined code.
2) How to clear the clipboard, so when ctrl-V is pressed there is nothing to paste. Something similar to Excel's "Application.CutCopyMode = Fales" would be perfect.

If e.Control Then
        If e.KeyCode = Keys.V Then
            ***RUN CUSTOM COMMAND HERE****
            Clipboard.SetDataObject(New DataObject)
        End If
    End If

Open in new window

2 Solutions
Jorge PaulinoIT Pro/DeveloperCommented:
Try this way:

If (Control.ModifierKeys = Keys.Control And e.KeyCode = Keys.V) Then
  e.SuppressKeyPress = True
  ' your code
End If
Mike TomlinsonMiddle School Assistant TeacherCommented:
How about:

    Dim tbp As New TextBoxPaste(Me.TextBox1)

(You can do a similar thing if you Inherit from TextBox as welll)

    Private Class TextBoxPaste
        Inherits NativeWindow

        Private tb As TextBox

        Private Sub New()
        End Sub

        Public Sub New(ByVal tb As TextBox)
            Me.tb = tb
        End Sub

        Private Const WM_PASTE As Integer = &H302

        Protected Overrides Sub WndProc(ByRef m As Message)
            Select Case m.Msg
                Case WM_PASTE
                    If Clipboard.GetDataObject.GetDataPresent(DataFormats.Text) Then
                        Dim str As String = Clipboard.GetDataObject.GetData(DataFormats.Text)
                        Debug.WriteLine("Pasted: " & str)

                        ' ...make a decision in here...

                        ' change "str" if you like

                        ' you can change what is pasted like this:
                        tb.SelectedText = str

                        ' If you want to cancel the paste, then use Exit Sub
                        Exit Sub
                    End If
            End Select

        End Sub
    End Class

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now