我处于这样一种情况,我想决定是否禁用或启用子类TextBox
控件的默认编辑上下文菜单,我写了这个:
Public Property DisableEditMenu As Boolean = False
''' <summary>
''' Raises the <see cref="E:System.Windows.Forms.Control.Enter"/> event.
''' </summary>
''' <param name="e">An <see cref="T:System.EventArgs"/> that contains the event data.</param>
Protected Overrides Sub OnEnter(e As EventArgs)
If Me.DisableEditMenu AndAlso MyBase.ContextMenuStrip Is Nothing Then
' Disable Copy/Cut/Paste contextmenu by creating a new empty one.
MyBase.ContextMenuStrip = New ContextMenuStrip
ElseIf Not Me.DisableEditMenu Then
' Restore default edit contextmenu...
End If
MyBase.OnEnter(e)
End Sub
问题是......如何恢复默认的contextmenu?
我认为以这种方式解决它,并且它可以正常工作,但我不确定我是否有效率或者我正在做不必要的事情而不是我应该做的恢复默认菜单:
...
ElseIf Not Me.DisableEditMenu Then
' Restore default edit contextmenu...
Using tb As New TextBox
MyBase.ContextMenuStrip = tb.ContextMenuStrip
End Using
...
因为 .Net Framework类库提供了默认值的引用,例如默认控件颜色(例如:TextBox.DefaultBackColor
),所以可能包含/引用默认编辑上下文菜单我没有发现的Type
?或者有效地恢复它比我想象的更难?
答案 0 :(得分:1)
要&#34;恢复默认的上下文菜单&#34; ,请将ContextMenuStrip
属性设置为null。
Me.ContextMenuStrip = Nothing
但是,没有必要将ContextMenu
设置为新的空实例。您所要做的就是禁止WM_CONTEXTMENU
消息。 MSDN将此消息描述为:
通知窗口用户在窗口中单击鼠标右键(右键单击)。
Public Class UITextBox
Inherits TextBox
Public Property DisableEditMenu As Boolean
Protected Overrides Sub WndProc(ByRef m As Message)
If ((m.Msg = WM_CONTEXTMENU) AndAlso Me.DisableEditMenu) Then
Exit Sub
Else
MyBase.WndProc(m)
End If
End Sub
Private Const WM_CONTEXTMENU As Integer = 123
End Class