Does anyone know how i can check when someone uses the standard paste function via rightmouseclick? I want to check somsthing before the text can be pasted.
Printable View
Does anyone know how i can check when someone uses the standard paste function via rightmouseclick? I want to check somsthing before the text can be pasted.
You could create a custom textbox that does this. It is pretty simple with inheritence:
Then to use it, create a MyTextBox on your form and then handle the BeforePaste Event:Code:Public Class MyTextBox
Inherits TextBox
Private Const WMPASTE As Integer = &H302
Public Event AfterPaste()
Public Event BeforePaste(ByRef Cancel As Boolean)
Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
Dim CancelPaste As Boolean = False
If (m.Msg = WMPASTE) Then
RaiseEvent BeforePaste(CancelPaste)
End If
If Not CancelPaste Then
MyBase.WndProc(m)
End If
If (m.Msg = WMPASTE) Then
RaiseEvent AfterPaste()
End If
End Sub
End Class
Code:Private Sub MyTextBox1_BeforePaste(ByRef CancelPaste As Boolean) Handles MyTextBox1.BeforePaste
If Clipboard.GetData(System.Windows.Forms.DataFormats.Text).ToString() = "abc" Then
CancelPaste = True
End If
End Sub
Hi dude, works fine. Thanks a lot. :thumb:
My code is now:
In my KeyDown procedure i can't use a messagebox to tell the user that he couldn't use more than 3 lines. If i use it, the e.handled and e.supresskeypress doesn't work. If someone any idea?Code:Private Sub txtFunctie_BeforePaste(ByRef Cancel As Boolean) Handles txtFunctie.BeforePaste
Dim strClipboardText() As String = Clipboard.GetText.Split(Environment.NewLine)
If strClipboardText.Length + txtFunctie.Lines.Length + 1 > 5 Then
Cancel = True
MsgBox("Works Fine")
End If
End Sub
Private Sub txtFunctie_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtFunctie.KeyDown
If (txtFunctie.Lines.Length + 1 > 3) And e.KeyCode = Keys.Enter Then
e.Handled = True
e.SuppressKeyPress = True
End If
End Sub
I changed to keypress and this works fine with an messagebox:
Code:Private Sub txtFunctie_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtFunctie.KeyPress
If (txtFunctie.Lines.Length + 1 > 3) And AscW(e.KeyChar) = Keys.Enter Then
e.KeyChar = String.Empty
MsgBox("Works Fine")
End If
End Sub