Solved

Ctrl+C, Ctrl+V handler

Posted on 2006-07-02
4
361 Views
Last Modified: 2011-04-14
Hi,

I am trying to implement cut/copy/paste functionality on my winforms using a common handler as follows:-

In Form Load event

   If ctl.CanSelect Then AddHandler ctl.KeyPress, AddressOf ctlKeyPress

In the ctlKeyPress subroutine

    Private Sub ctlKeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs)
        If TypeOf sender Is TextBox Then
            If e.keychar = Convert.ToChar(84) Then
                Clipboard.SetText(CType(sender, TextBox).Text)
            End If
            If e.KeyChar = Convert.ToChar(103) Then
                sender.text = Clipboard.GetText
            End If
        End If

        If TypeOf sender Is ComboBox Then
            If e.KeyChar = Convert.ToChar(84) Then
                Clipboard.SetText(CType(sender, ComboBox).Text)
            End If
            If e.KeyChar = Convert.ToChar(103) Then
                sender.text = Clipboard.GetText
            End If

        End If

  End Sub

The addhandler routine works OK (seems to anyway) the the form loads with no errors. When run and pressing Ctrl+C to copy text from one texbox to another, the ctlKeyPress sub does not fire.  Any ideas?

Also, the convert.tochars values (84 / 103) are what other code gave me as the copy/paste keycodes.  Other posts quote other numbers. What are the correct values?

I want to implement this to avoid retaining the users to use the right context menu option to copy/paste.

Regards,


Alan



0
Comment
Question by:alan_ITG
  • 2
4 Comments
 
LVL 48

Assisted Solution

by:AlexFM
AlexFM earned 200 total points
ID: 17026286
This functionality works by default without any code, you can remove your function.
0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 300 total points
ID: 17026560
Use the technique I present here:
http://www.experts-exchange.com/Programming/Programming_Languages/Dot_Net/VB_DOT_NET/Q_21741197.html

But instead of trapping WM_PASTE, trap the WM_RBUTTONDOWN msg:

        Private Const WM_RBUTTONDOWN As Integer = &H204

        Protected Overrides Sub WndProc(ByRef m As Message)
            Select Case m.Msg
                Case WM_RBUTTONDOWN
                        ' Suppress the default right click context menu
                        Exit Sub
                    End If
            End Select

            MyBase.WndProc(m)
        End Sub
0
 
LVL 85

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 300 total points
ID: 17026567
You can disable copying/pasting in the TextBox by adding the WM_COPY and WM_PASTE to the Case statement:

        Private Const WM_PASTE As Integer = &H302
        Private Const WM_COPY As Integer = &H300
        Private Const WM_RBUTTONDOWN As Integer = &H204

        Protected Overrides Sub WndProc(ByRef m As Message)
            Select Case m.Msg
                Case WM_PASTE, WM_COPY, WM_RBUTTONDOWN
                        ' Suppress the default right click context menu
                        ' AND any copy/paste attempts
                        Exit Sub
                    End If
            End Select

            MyBase.WndProc(m)
        End Sub
0
 

Author Comment

by:alan_ITG
ID: 17071526

Thanks for your input.

Alan
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

813 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

18 Experts available now in Live!

Get 1:1 Help Now