Solved

Ctrl+C, Ctrl+V handler

Posted on 2006-07-02
4
342 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility

Thanks for your input.

Alan
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
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…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

772 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

12 Experts available now in Live!

Get 1:1 Help Now