|
-
Mar 19th, 2009, 03:02 PM
#1
Thread Starter
Member
[RESOLVED] Check when paste function selected for a textbox
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.
-
Mar 19th, 2009, 03:31 PM
#2
Re: Check when paste function selected for a textbox
You could create a custom textbox that does this. It is pretty simple with inheritence:
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
Then to use it, create a MyTextBox on your form and then handle the BeforePaste Event:
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
-
Mar 19th, 2009, 05:20 PM
#3
Thread Starter
Member
Re: Check when paste function selected for a textbox
Hi dude, works fine. Thanks a lot. 
My code is now:
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
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?
-
Mar 19th, 2009, 05:26 PM
#4
Thread Starter
Member
Re: Check when paste function selected for a textbox
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
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|