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.

TIA,
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.
i.e.
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
System.Windows.Forms.Message)

Select Case m.Msg

Case WM_CUT

Clipboard.SetDataObject(Me.SelectedText +
Me.SelectedText)
Dim startSel As Integer = Me.SelectionStart
Dim startRemain As Integer = startSel +
Me.SelectionLength
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
Case WM_COPY

Clipboard.SetDataObject(Me.SelectedText +
Me.SelectedText)
Case WM_PASTE

Dim data As IDataObject = Clipboard.GetDataObject()

Dim tmpData As String =
CStr(data.GetData(GetType(String)))
tmpData += tmpData

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

Case Else

MyBase.WndProc(m)

End Select

End Sub

End Class

Alan.

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.