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

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.