Solved

how can i tell when in a Textbox in  microsoft VS 2008 I did a mousedown copy or mousedown paste?

Posted on 2011-09-27
17
380 Views
Last Modified: 2012-05-12
vs 2008 .net
in a textbox what events will tell me if i did a mousedown copy or mousedown paste
I have 8 testboxs on a form. If i  mousedown and paste i want to clean out all the other 7 textboxes. I have that code to clean the textboxs but i need to know what events and what keychars i look for  when a mousedown copy or mouse down paste

if ie if i want to if i did a control v i look for
(e.KeyCode = Keys.V And e.Modifiers = Keys.Control) The

i
0
Comment
Question by:john M
17 Comments
 
LVL 40
ID: 36712240
You were going in the right direction your piece of code, but the KeyUp event is the easiest to use:
Private Sub TextBox1_KeyUp(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyUp
   If e.KeyCode = Keys.V And e.Control Then
      'You got a paste
   End If
End Sub

Open in new window

There is a problem however. The user can also paste with the default ContextMenu of the TextBox. And since you do not provide that ContextMenu, there is no way (as far as I know), to detect if information was pasted or copied from that menu.

So you will also need to either recreate the ContextMenu, or easier (but maybe your users won't like this), create an empty ContextMenu and assign it to the TextBox. That disables the ContextMenu.
0
 
LVL 3

Author Comment

by:john M
ID: 36712510
so if i understand you there is no way to use the mousedown event amd check for paste or copy
in the keydown event i test for ctrl V and this works but no way to use the mousedown to test for a char pasted in the textbox?
0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 36712536
So you want to clear out the other TextBoxes when one of them gets something Pasted to it?

Here is an example that will trap WM_PASTE whether it was from Ctrl-V or via the built-in Context Menu (mouse):
Public Class Form1

    Private TBs As New List(Of TextBox)
    Private TBCPs As New List(Of TextBoxCopyPaste)

    Private Sub Form1_Shown(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Shown
        TBs.AddRange(New TextBox() {TextBox1, TextBox2, TextBox3, TextBox4, TextBox5, TextBox6, TextBox7, TextBox8})
        For Each TB As TextBox In TBs
            Dim tbcp As New TextBoxCopyPaste(TB)
            AddHandler tbcp.Pasted, AddressOf TBPCP_Pasted
            TBCPs.Add(tbcp)
        Next
    End Sub

    Private Sub TBPCP_Pasted(ByVal SourceTB As System.Windows.Forms.TextBox)
        Debug.Print("Pasted into " & SourceTB.Name)

        ' Clear out all the other TextBoxes:
        For Each TB As TextBox In TBs
            If Not (TB Is SourceTB) Then
                TB.Clear()
            End If
        Next
    End Sub

End Class

Public Class TextBoxCopyPaste
    Inherits NativeWindow

    Private tb As TextBox

    Public Event Pasted(ByVal TB As TextBox)

    Private Sub New()
    End Sub

    Public Sub New(ByVal tb As TextBox)
        If Not IsNothing(tb) Then
            Me.tb = tb
            Me.AssignHandle(tb.Handle)
        Else
            MessageBox.Show("Nothing Passed In!")
        End If        
    End Sub

    Private Const WM_PASTE As Integer = &H302

    Protected Overrides Sub WndProc(ByRef m As Message)
        Select Case m.Msg
            Case WM_PASTE
                RaiseEvent Pasted(Me.tb)

        End Select

        MyBase.WndProc(m)
    End Sub

End Class

Open in new window

0
 
LVL 15

Expert Comment

by:x77
ID: 36712572
Keys Are :  Ctrl-V and Shift-Insert
Note that I use Code Like JamesBurger (KeyUp), but I use most Precise : e.modifiers = Key.Control.
This avoids   Ctrl-Sift-Alt-V  (By Sample)

I Add also "Sender is nothing" to use with aditional class to capture message WM_PASTEThis message is received by TextBox when user right-Click mouse over TextBox and select Paste-Menu.


    Private Sub EdCtl_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs)
        If (sender Is Nothing OrElse _
            (e.Modifiers = Keys.Control AndAlso e.KeyCode = Keys.V) OrElse _
            (e.Modifiers = Keys.Shift AndAlso e.KeyCode = Keys.Insert)) Then

 
Public Class PasteCapture
    Inherits System.Windows.Forms.NativeWindow
    Public method As System.Windows.Forms.KeyEventHandler

    Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
        Const WM_PASTE As Integer = &H302
        MyBase.WndProc(m)
        If m.Msg = WM_PASTE AndAlso method IsNot Nothing Then method(Nothing, Nothing)
    End Sub
End Class

Open in new window


I use a instance from this class to capture messages on active control:

    Private PasteCapture As New PasteCapture with { .method = AddressOf Me.EdCtl_KeyUp}

To Begin Capture (Got Focus by sample):
    PasteCapture.AssignHandle(EdCtl.Handle)

To Release Capture (Lost Focus by sample):
    If PasteCapture.Handle <> IntPtr.Zero Then PasteCapture.ReleaseHandle()

Note that this class can only subclass one window.
0
 
LVL 3

Author Comment

by:john M
ID: 36712615
idea mind
well i have a  form in production its has 8 textboxs
we then search our DB out of what is entered

i wanted to capture either mousedown copy or mousedown paste when it is done in any of the textboxes - i tired in the mousedown event but it limits the ability to look at keys enterned

i allready in the keydown event capture ctrl V

so yours can check any of the textboxs and tell me is ctlr V is done?
can it check for mousedown copy or mousedown paste
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 36712696
Mine will tell you if a paste operation has occurred in any of the TextBoxes for both keyboard and mouse.

You can do the exact same thing, trapping WM_COPY, by adding this constant and Event:

    Private Const WM_COPY As Integer = &H301

    Public Event Copied(ByVal TB As TextBox)

Then add it into WndProc() like this:

    Protected Overrides Sub WndProc(ByRef m As Message)
        Select Case m.Msg
            Case WM_PASTE
                RaiseEvent Pasted(Me.tb)

            Case WM_COPY
                RaiseEvent Copied(Me.tb)

        End Select

        MyBase.WndProc(m)
    End Sub

Don't forget to wire it up in the Form with AddHandler():

            AddHandler tbcp.Copied, AddressOf TBPCP_Copied

All together, it would look like this:
Public Class Form1

    Private TBs As New List(Of TextBox)
    Private TBCPs As New List(Of TextBoxCopyPaste)

    Private Sub Form1_Shown(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Shown
        TBs.AddRange(New TextBox() {TextBox1, TextBox2, TextBox3, TextBox4, TextBox5, TextBox6, TextBox7, TextBox8})
        For Each TB As TextBox In TBs
            Dim tbcp As New TextBoxCopyPaste(TB)
            AddHandler tbcp.Pasted, AddressOf TBPCP_Pasted
            AddHandler tbcp.Copied, AddressOf TBPCP_Copied
            TBCPs.Add(tbcp)
        Next
    End Sub

    Private Sub TBPCP_Pasted(ByVal SourceTB As System.Windows.Forms.TextBox)
        Debug.Print("Pasted into " & SourceTB.Name)

        ' Clear out all the other TextBoxes:
        For Each TB As TextBox In TBs
            If Not (TB Is SourceTB) Then
                TB.Clear()
            End If
        Next
    End Sub

    Private Sub TBPCP_Copied(ByVal SourceTB As System.Windows.Forms.TextBox)
        Debug.Print("Copied from " & SourceTB.Name)

        ' ... do something in here ...
    End Sub

End Class

Public Class TextBoxCopyPaste
    Inherits NativeWindow

    Private tb As TextBox

    Public Event Pasted(ByVal TB As TextBox)
    Public Event Copied(ByVal TB As TextBox)

    Private Sub New()
    End Sub

    Public Sub New(ByVal tb As TextBox)
        If Not IsNothing(tb) Then
            Me.tb = tb
            Me.AssignHandle(tb.Handle)
        Else
            MessageBox.Show("Nothing Passed In!")
        End If        
    End Sub

    Private Const WM_PASTE As Integer = &H302
    Private Const WM_COPY As Integer = &H301

    Protected Overrides Sub WndProc(ByRef m As Message)
        Select Case m.Msg
            Case WM_PASTE
                RaiseEvent Pasted(Me.tb)

            Case WM_COPY
                RaiseEvent Copied(Me.tb)

        End Select

        MyBase.WndProc(m)
    End Sub

End Class

Open in new window

0
 
LVL 3

Author Comment

by:john M
ID: 36712814
idle

thanks

so to make this work i take
this code
Public Class TextBoxCopyPaste
    Inherits NativeWindow

    Private tb As TextBox

    Public Event Pasted(ByVal TB As TextBox)
    Public Event Copied(ByVal TB As TextBox)

    Private Sub New()
    End Sub

    Public Sub New(ByVal tb As TextBox)
        If Not IsNothing(tb) Then
            Me.tb = tb
            Me.AssignHandle(tb.Handle)
        Else
            MessageBox.Show("Nothing Passed In!")
        End If        
    End Sub

    Private Const WM_PASTE As Integer = &H302
    Private Const WM_COPY As Integer = &H301

    Protected Overrides Sub WndProc(ByRef m As Message)
        Select Case m.Msg
            Case WM_PASTE
                RaiseEvent Pasted(Me.tb)

            Case WM_COPY
                RaiseEvent Copied(Me.tb)

        End Select

        MyBase.WndProc(m)
    End Sub

End Class
Toggle HighlightingOpen in New Window

Put it at the bottom of my expisting code



then  these 2 line go up where i declare my varibles
Private TBs As New List(Of TextBox)
    Private TBCPs As New List(Of TextBoxCopyPaste)

then the rest goes in my application
Private Sub Form1_Shown(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Shown
        TBs.AddRange(New TextBox() {TextBox1, TextBox2, TextBox3, TextBox4, TextBox5, TextBox6,
TextBox7, TextBox8})
        For Each TB As TextBox In TBs
            Dim tbcp As New TextBoxCopyPaste(TB)
            AddHandler tbcp.Pasted, AddressOf TBPCP_Pasted
            AddHandler tbcp.Copied, AddressOf TBPCP_Copied
            TBCPs.Add(tbcp)
        Next
    End Sub

    Private Sub TBPCP_Pasted(ByVal SourceTB As System.Windows.Forms.TextBox)
        Debug.Print("Pasted into " & SourceTB.Name)

        ' Clear out all the other TextBoxes:
        For Each TB As TextBox In TBs
            If Not (TB Is SourceTB) Then
                TB.Clear()
            End If
        Next
    End Sub

    Private Sub TBPCP_Copied(ByVal SourceTB As System.Windows.Forms.TextBox)
        Debug.Print("Copied from " & SourceTB.Name)

        ' ... do something in here ...
    End Sub

i change these 2 lines to
        TBs.AddRange(New TextBox() {TextBox1, TextBox2, TextBox3, TextBox4, TextBox5, TextBox6,
TextBox7, TextBox8})

have the names of my textboxes

thanks in advance for all this great help
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 36713490
Did you get it to work for you then?...
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 3

Author Comment

by:john M
ID: 36717583
sorry i go home at 3:45  - Back workign at it now
one thing i do see is 2 of my textboxs are really maskedtextboxes so i took them out of the
the list of text boxs - back working on it now
0
 
LVL 3

Author Closing Comment

by:john M
ID: 36717796
Wow you rule !!!!!!!!!!!!!!!!!!!
Works great the only issue is as i said 2 of my textbox are masktextboxes
and  i get this error
value of type systems .window.form maskedtextbox cann't be converted to
system.window.form.textbox'
if you have any ideas about this it would help  but thanks so much for your help
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 36717967
Both TextBox and MaskedTextBox Inherit from TextBoxBase:
Public Class Form1

    Private TBBs As New List(Of Control)
    Private TBBCPs As New List(Of TextBoxBaseCopyPaste)

    Private Sub Form1_Shown(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Shown
        TBBs.AddRange(New TextBoxBase() {TextBox1, TextBox2, TextBox3, TextBox4, TextBox5, TextBox6, MaskedTextBox1, MaskedTextBox2})
        For Each TBB As TextBoxBase In TBBs
            Dim TBBCP As New TextBoxBaseCopyPaste(TBB)
            AddHandler TBBCP.Pasted, AddressOf TBBCP_Pasted
            AddHandler TBBCP.Copied, AddressOf TBBCP_Copied
            TBBCPs.Add(TBBCP)
        Next
    End Sub

    Private Sub TBBCP_Pasted(ByVal SourceTBB As System.Windows.Forms.TextBoxBase)
        Debug.Print("Pasted into " & SourceTBB.Name)

        ' Clear out all the other TextBoxBases:
        For Each TBB As TextBoxBase In TBBs
            If Not (TBB Is SourceTBB) Then
                TBB.Clear()
            End If
        Next
    End Sub

    Private Sub TBBCP_Copied(ByVal SourceTBB As System.Windows.Forms.TextBoxBase)
        Debug.Print("Copied from " & SourceTBB.Name)

        ' ... do something in here ...
    End Sub

End Class

Public Class TextBoxBaseCopyPaste
    Inherits NativeWindow

    Private TBB As TextBoxBase

    Public Event Pasted(ByVal TBB As TextBoxBase)
    Public Event Copied(ByVal TBB As TextBoxBase)

    Private Sub New()
    End Sub

    Public Sub New(ByVal TBB As TextBoxBase)
        If Not IsNothing(TBB) Then
            Me.TBB = TBB
            Me.AssignHandle(TBB.Handle)
        End If
    End Sub

    Private Const WM_PASTE As Integer = &H302
    Private Const WM_COPY As Integer = &H301

    Protected Overrides Sub WndProc(ByRef m As Message)
        Select Case m.Msg
            Case WM_PASTE
                RaiseEvent Pasted(Me.TBB)

            Case WM_COPY
                RaiseEvent Copied(Me.TBB)

        End Select

        MyBase.WndProc(m)
    End Sub

End Class

Open in new window

0
 
LVL 3

Author Comment

by:john M
ID: 36718014
Thanks  i greatly appreate your wisdom   I wish i could give you 5 million points
0
 
LVL 3

Author Comment

by:john M
ID: 36718179
Sorry but getting a error

 

    Private Sub Form1_Shown(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Shown
ERROR occurs here --->>>       TBBs.AddRange(New TextBoxBase() {txtDANBID, txtCENumber, txtLastName, txtFirstName, txtCity, txtEmail, txtSearchBy}) ', txtSSN1, txtTelePhone1})
 
Sorry but getting a error

Unable to cast object from a number , the valuse must be less then infinity

unable to cast object of type 'system.windows.fprms/textboxbase{} to type 'system.collection.generic.IEnumerable '1{system.windows.form.textbox

       For Each TBB As TextBoxBase In TBBs
            Dim TBBCP As New TextBoxBaseCopyPaste(TBB)
            AddHandler TBBCP.Pasted, AddressOf TBBCP_Pasted
            'AddHandler TBBCP.  , AddressOf TBBCP_Copied
            TBBCPs.Add(TBBCP)


stack trace

at CentreTrak.frmDemogSearch.Form1_Shown(Object sender, EventArgs e)    at System.Windows.Forms.Form.OnShown(EventArgs e)    at System.Windows.Forms.Form.CallShownEvent()    at System.Windows.Forms.Control.InvokeMarshaledCallbackDo(ThreadMethodEntry tme)    at System.Windows.Forms.Control.InvokeMarshaledCallbackHelper(Object obj)    at System.Threading.ExecutionContext.runTryCode(Object userData)    at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData)    at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)    at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)    at System.Windows.Forms.Control.InvokeMarshaledCallback(ThreadMethodEntry tme)    at System.Windows.Forms.Control.InvokeMarshaledCallbacks()    at System.Windows.Forms.Control.WndProc(Message& m)    at System.Windows.Forms.ScrollableControl.WndProc(Message& m)    at System.Windows.Forms.ContainerControl.WndProc(Message& m)    at System.Windows.Forms.Form.WndProc(Message& m)    at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)    at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)    at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)    at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)    at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)    at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)    at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)    at System.Windows.Forms.Application.Run(Form mainForm)    at CentreTrak.frmDemogSearch.Main()    at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)    at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)    at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()    at System.Threading.ThreadHelper.ThreadStart_Context(Object state)    at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)    at System.Threading.ThreadHelper.ThreadStart()"
        Next
    End Sub
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 36718248
Try changing the declaration of TBBs to:

    Private TBBs As New List(Of TextBoxBase)

Otherwise I'm not sure and would need to see more code.  It worked fine in my test project.
0
 
LVL 3

Author Comment

by:john M
ID: 36718296
Thanks again works great -can i ask where did you learn to do stuff like this?
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 36718332
I'm mostly self taught thru the school of hard knocks (progressed from BASICA to VB6 to VB.Net).  I learn a lot of new tricks, though, by reading articles on the web and exploring solutions here on Experts-Exchange.
0
 
LVL 3

Author Comment

by:john M
ID: 36718649
like i am doing thanks
old cobol programmer 30 years now going to .net /sql /CR
again thanks for being so generous
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now