?
Solved

Ctrl+C, Ctrl+V handler

Posted on 2006-07-02
4
Medium Priority
?
395 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 800 total points
ID: 17026286
This functionality works by default without any code, you can remove your function.
0
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 1200 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 86

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 1200 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses
Course of the Month14 days, 17 hours left to enroll

839 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