ContextMenuStrip Problems
Hello Everyone,
VS BETA 2
I have a MenuStrip with the standard items. I wish to create a contextmenu dynamically with the edit menu dropdown items in it. Obviously I don't wish to duplicate event handlers and of course I don't want to hard code the creation of the items.
Why does the contextmenustrip remove items from the original list when adding them to the contextmenustrip.
columnDataGridViewContextMenuStrip.Items.Add
Is there any work around just to reference these items without duplicating them for each contextmenustrip?
I am sure I am missing something.... :-)
Also of interest is that adding a seperator to a contextmenustrip programmatically does not work either when added normally or using the "Text" property. A blank entry is displayed instead.
Darren
This is normal, a ToolStripItem can only exist on one toolstrip. Otherwise, if you called something like ToolStripItem.Select() how would it know which item to select?
To add a separater create a ToolStripSeparator and add it to the ContextMenuStrip.
I found a way to restore the VB6 style of using a context menu from the main menu. Which allows you to call the menu from the MouseUp event, in the control you want to reuse the main menu in.
Steps:
-Add the Class Listed below to your project.
-Populate the Main Menu as normal, EXCEPT use the Class below, "ContextMenuItem", instead of the normal ToolStripMenuItem to create the parents you want to reuse as context menus.
-in the control you will to use the menu with inside the MouseUp routine place code like the following...
if e.Button=Right then
ContextMenuItem1.ShowDropMenu(true)
end if
-Lastly DONT use the ContextMenu Property in the forms designer on that control because they will conflict.
| | Public Class ContextMenuItem Inherits ToolStripMenuItem 'CODE by Darren M. Bork, free to use because MS pissed me off too much with lost features in VB6. :) Private pContext As Point 'specific point for the context menu instead of the default screen position. Private bIsContext As Boolean = False 'tracks if the next display show be with different coordinates. ''' <summary> ''' Shows the specified ContextMenuItem ''' </summary> ''' <param name="bContext">bContext=True - displays the context menu at the current screen co-ordinates</param> ''' <remarks></remarks> Shadows Sub ShowDropDown(ByVal bContext As Boolean) If bContext Then bIsContext = True Else bIsContext = False End If pContext = Nothing MyBase.ShowDropDown() End Sub ''' <summary> ''' Shows the specified ContextMenuItem at the screen point specified ''' </summary> ''' <param name="pSpecificScreenPoint">The Point to display the menu at, if Nothing the MousePosition is used.</param> ''' <remarks></remarks> Shadows Sub ShowDropDown(ByVal pSpecificScreenPoint As Point) bIsContext = True pContext = pSpecificScreenPoint MyBase.ShowDropDown() End Sub ''' <summary> ''' Shows the specified ContextMenuItem at the default position in the menu. ''' </summary> ''' <remarks></remarks> Shadows Sub ShowDropDown() bIsContext = False MyBase.ShowDropDown() End Sub ''' <summary> ''' Overriden to allow reset of the Context option that informas the control to display as context. ''' </summary> ''' <param name="e"></param> ''' <remarks></remarks> Protected Overrides Sub OnMouseEnter(ByVal e As System.EventArgs) bIsContext = False MyBase.OnMouseEnter(e) End Sub ''' <summary> ''' Overriden to change the location of drawing the menu based on the developers request. ''' </summary> ''' <value></value> ''' <returns>The Point to draw the menu at</returns> ''' <remarks></remarks> Protected Overrides ReadOnly Property DropDownLocation() As System.Drawing.Point Get Dim pBase As Point = MyBase.DropDownLocation Dim pOut As Point If Not bIsContext Then 'normal render of menu. pOut = pBase ElseIf pContext = Nothing Then 'context menu at the current mouse co-ord's pOut = Control.MousePosition() Else 'context menu at the point specified in the function call. pOut = pContext End If Return pOut End Get End Property End Class |
Darren M. Bork