Results 1 to 4 of 4

Thread: [RESOLVED] Check when paste function selected for a textbox

  1. #1

    Thread Starter
    Member
    Join Date
    May 2008
    Posts
    32

    Resolved [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.

  2. #2
    PowerPoster 2.0 Negative0's Avatar
    Join Date
    Jun 2000
    Location
    Southeastern MI
    Posts
    4,367

    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

  3. #3

    Thread Starter
    Member
    Join Date
    May 2008
    Posts
    32

    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?

  4. #4

    Thread Starter
    Member
    Join Date
    May 2008
    Posts
    32

    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
  •  



Click Here to Expand Forum to Full Width