Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Ctrl+C, Ctrl+V handler

Posted on 2006-07-02
4
Medium Priority
?
391 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Industry Leaders: 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

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

636 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