I have created a custom toolstrip combobox to allow it display a context menu. Here is the code.
Option Strict Off
Option Explicit On
<System.ComponentModel.Description("Custom Control to add a context menu to a toolstrip combobox")> _
Public Class MyToolStripComboBox
Inherits Forms.ToolStripComboBox
Private mContextMenu As ContextMenuStrip
' allows a context menu to be added to a toolstrip combobox control
Public Property ContextMenuStrip() As ContextMenuStrip
Get
Return mContextMenu
End Get
Set(ByVal iConMenu As ContextMenuStrip)
mContextMenu = iConMenu
End Set
End Property
Protected Overrides Sub OnMouseDown(e As System.Windows.Forms.MouseEventArgs)
MyBase.OnMouseDown(e)
If e.Button = MouseButtons.Right Then
If mContextMenu IsNot Nothing Then
Dim iBounds = Me.Bounds
Dim iParLoc As Point = Parent.Location
Dim iPt As Point = iParLoc
iPt.X += iBounds.Left
iPt.Y += iBounds.Bottom
mContextMenu.Show(Parent.PointToScreen(iPt))
End If
End If
End Sub
End Class
It works fine except that the default context menu strip (i.e. cut, copy, paste...) displays over the top of it.
How do I disable the default context menu strip?
I have determined the source of the issue. With the above code setting the Y coordinate, the default context menu will display on top of the control specific context menu (mContextMenu). If this line is remarked out or if the Y coordinate is set to
e.Location.Y
the default context menu will not display. Also, if mContextMenu is set display at
e.Location
mContextMenu.Show(Parent.PointToScreen(e.Location))
, the default context menu will display at the control and mContextMenu will display at the location on the containing toolstrip (at the left end of the toolstrip). I do not know why this is. I wish someone could explain this behavior to me.
The problem with this is that the user cannot see the contents of the control when the context menu is displayed. It is right on top of the control.
What I have tried:
On the form that uses this control I have set the toolstrip of the container to a new (blank) context menu at startup:
tsMain.ContextMenuStrip = New ContextMenuStrip
Also, I have created a new blank context menu and assigned this to tsMain in design.
Neither of these methods prevents the default context menu strip from display when I right-click on the item.