次のようなクラスをプロジェクトに追加できます。
Class MyTextBox : Inherits TextBox
Public Enum ContextCommands
WM_CUT = &H300
WM_COPY = &H301
WM_PASTE = &H302
End Enum
Public Class ContextCommandEventArgs
Inherits EventArgs
Public Property Command As ContextCommands
End Class
Event OnCut(sender As Object, e As ContextCommandEventArgs)
Event OnCopy(sender As Object, e As ContextCommandEventArgs)
Event OnPaste(sender As Object, e As ContextCommandEventArgs)
Protected Overrides Sub WndProc(ByRef m As Message)
MyBase.WndProc(m)
Select Case m.Msg
Case ContextCommands.WM_CUT
RaiseEvent OnCut(Me, New ContextCommandEventArgs() With {.Command = ContextCommands.WM_CUT})
Case ContextCommands.WM_COPY
RaiseEvent OnCopy(Me, New ContextCommandEventArgs() With {.Command = ContextCommands.WM_COPY})
Case ContextCommands.WM_PASTE
RaiseEvent OnPaste(Me, New ContextCommandEventArgs() With {.Command = ContextCommands.WM_PASTE})
End Select
End Sub
End Class
次に、Designer.vb ファイル内のすべての "TextBox" を "MyTextBox" に置き換えることができます。次に、カット、コピー、ペーストの 3 つの新しいイベントにアクセスできます。次のように処理できます。
Private Sub TextBox1_OnTextCommand(sender As Object, e As MyTextBox.ContextCommandEventArgs) _
Handles TextBox1.OnCut, TextBox1.OnPaste, TextBox1.OnCopy
MessageBox.Show("Activated " & e.Command.ToString())
End Sub
この場合、3 つのイベントすべてを 1 つの関数で処理することを選択したことに注意してください。ただし、それらを別の関数で処理することもできます。切り取りコマンドはコピー コマンド イベントも発生させるように見えますが、ここでは、そのわずかな複雑さに対処できると仮定します。