469,950 Members | 1,871 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,950 developers. It's quick & easy.

TextBoxes, Context Menus, Copying and Pasting

(Apologies if I've missed the answer someplace else, but ... )

Is it possible in "intercept" the actions of the standard (Cut'n'Paste)
context menu provided by a TextBox (or derived class thereof) to
manipulate the data going to and/or from the Clipboard?

Short of completely recplacing the standard one with a Context
menu of my own, I can't find any way of doing this.

Phill W.
Nov 21 '05 #1
1 969

You can create a derived class and override the WndProc method

The code below will 'double' any code being cut/copied from the textbox
or pasted into it.
If the textbox contains STRING and you cut/copy it and paste it else
where you will see STRINGSTRING in the new location.
if you copy VALUE from somewhere else and paste it, you will see
VALUEVALUE in the textbox.
Public Class MyTextBox
Inherits System.Windows.Forms.TextBox

<< #Region " Windows Form Designer generated code ">>

Private Const WM_CUT As Long = &H300
Private Const WM_COPY As Long = &H301
Private Const WM_PASTE As Long = &H302

Protected Overrides Sub WndProc(ByRef m As

Select Case m.Msg


Clipboard.SetDataObject(Me.SelectedText +
Dim startSel As Integer = Me.SelectionStart
Dim startRemain As Integer = startSel +
Dim lenRemain As Integer = Me.Text.Length - startRemain

Dim strRemain As String = Me.Text.Substring(0,
startSel) + _

Me.Text.Substring(startRemain, lenRemain)

Me.Text = strRemain
Me.SelectionStart = startSel
Me.SelectionLength = 0

Clipboard.SetDataObject(Me.SelectedText +

Dim data As IDataObject = Clipboard.GetDataObject()

Dim tmpData As String =
tmpData += tmpData

Me.Text = tmpData
Me.Select(Me.Text.Length, 0)

Case Else


End Select

End Sub

End Class


Nov 21 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Rory | last post: by
2 posts views Thread by Patryk | last post: by
1 post views Thread by Doug Bell | last post: by
reply views Thread by Robert Smith | last post: by
reply views Thread by tony.spalding | last post: by
2 posts views Thread by Ron M. Newman | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.