TextBoxes, Context Menus, Copying and Pasting

P

Phill. W

(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.
 
A

alantolan

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top