Click here to Skip to main content
15,867,568 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
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.
Posted
Updated 17-Jan-18 15:21pm
v3
Comments
Kevin Brady 19-Jan-18 17:47pm    
I posted a new question on using this. https://www.codeproject.com/Questions/1226184/Get-the-embedded-source-control-object

1 solution

I think the problem is that the new ContextMenuStrip has not been assigned to the ToolStripComboBox. Once that is done it will just work and the MouseDown handler and positioning code will be unnecessary.

The ToolStripComboBox is a fairly simple wrapper around a System.Windows.Forms.ComboBox and it does not expose all of the underlying functionality but there is a property which does. Find the ToolStripComboBox.ComboBox.ContextMenuStrip property and stuff the new menu in there. Once that has been done the old fashioned native menu will be gone.

Alan.
 
Share this answer
 
Comments
Kevin Brady 18-Jan-18 10:05am    
The context menu strip HAS been assigned to the custom toolstripcombobox control in the designer. This is not the problem. Please re-read my description including the new information I presented yesterday. In addition to the original question, perhaps the more fundamental question is where in the application does the default toolstrip derive from?
Alan N 18-Jan-18 15:16pm    
A ComboBox is a combination of a TextBox and a ListBox and that default menu comes from the win32 EditControl (TextBox) part. I've always thought it was a shame that Microsoft did not remove that menu from the .NET textbox and combobox as it looks really out of place and cannot be customised, except by replacement.

I see you are certain that you have assigned the menu correctly. Could you post the designer code where that is done?
Kevin Brady 18-Jan-18 15:09pm    
I investigated this further and Alan N is correct. I modified the original custom class to remove the OnMouse down method and assigned the context menu directly to the underlying ComboBox. It works as expected.

Public Property ContextMenuStrip() As ContextMenuStrip
Get
Return Me.ComboBox.ContextMenuStrip
End Get
Set(ByVal iConMenu As ContextMenuStrip)
Me.ComboBox.ContextMenuStrip = iConMenu
End Set
End Property

Thanks Alan N.
Alan N 18-Jan-18 15:17pm    
Ignore my last comment. We must have been editing at about the same time.
Kevin Brady 19-Jan-18 17:06pm    
Now, on to how to use it. I will be posting another thread soon.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900