Solved

Ctrl+C, Ctrl+V handler

Posted on 2006-07-02
4
385 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 200 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 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 86

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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
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…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

705 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