I want to show contextMenu on Menu Item Right click..But probs is dat when i right click on menu Item,Context Menu is popping up..But menu disapper..I want that menu should not be disappered...
See in Attachment

Private Sub MenuAddToFavorites_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MenuAddToFavorites.MouseDown

        If e.Button = Windows.Forms.MouseButtons.Right Then
            'ContextMenuFavorites.Show(e.Location)
            ContextMenuFavorites.Show(DirectCast(sender, ToolStripMenuItem).GetCurrentParent.PointToScreen(e.Location))
        End If
    End Sub

Recommended Answers

All 2 Replies

I hope this code will help you.

Public Class Form1
    Private Class CustomToolStripMenuItem
        Inherits ToolStripMenuItem
        Private m_secondaryContextMenu As ContextMenuStrip
        Public Property SecondaryContextMenu() As ContextMenuStrip
            Get
                Return m_secondaryContextMenu
            End Get
            Set(ByVal value As ContextMenuStrip)
                m_secondaryContextMenu = value
            End Set
        End Property
        Public Sub New(ByVal text As String)
            MyBase.New(text)
        End Sub
        Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
            If disposing Then
                If m_secondaryContextMenu IsNot Nothing Then
                    m_secondaryContextMenu.Dispose()
                    m_secondaryContextMenu = Nothing
                End If
            End If
            MyBase.Dispose(disposing)
        End Sub
        Protected Overloads Overrides Sub OnClick(ByVal e As EventArgs)
            If SecondaryContextMenu Is Nothing OrElse MouseButtons <> MouseButtons.Right Then
                MyBase.OnClick(e)
            End If
        End Sub
    End Class
    Private Class CustomContextMenuStrip
        Inherits ContextMenuStrip
        Private secondaryContextMenuActive As Boolean = False
        Private lastShownSecondaryContextMenu As ContextMenuStrip = Nothing
        Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
            If disposing Then
                If lastShownSecondaryContextMenu IsNot Nothing Then
                    lastShownSecondaryContextMenu.Close()
                    lastShownSecondaryContextMenu = Nothing
                End If
            End If
            MyBase.Dispose(disposing)
        End Sub
        Protected Overloads Overrides Sub OnControlAdded(ByVal e As ControlEventArgs)
            AddHandler e.Control.MouseClick, AddressOf Control_MouseClick
            MyBase.OnControlAdded(e)
        End Sub
        Protected Overloads Overrides Sub OnControlRemoved(ByVal e As ControlEventArgs)
            RemoveHandler e.Control.MouseClick, AddressOf Control_MouseClick
            MyBase.OnControlRemoved(e)
        End Sub
        Private Sub Control_MouseClick(ByVal sender As Object, ByVal e As MouseEventArgs)
            ShowSecondaryContextMenu(e)
        End Sub
        Protected Overloads Overrides Sub OnMouseClick(ByVal e As MouseEventArgs)
            ShowSecondaryContextMenu(e)
            MyBase.OnMouseClick(e)
        End Sub
        Private Function ShowSecondaryContextMenu(ByVal e As MouseEventArgs) As Boolean
            Dim ctsm As CustomToolStripMenuItem = TryCast(Me.GetItemAt(e.Location), CustomToolStripMenuItem)
            If ctsm Is Nothing OrElse ctsm.SecondaryContextMenu Is Nothing OrElse e.Button <> MouseButtons.Right Then
                Return False
            End If
            lastShownSecondaryContextMenu = ctsm.SecondaryContextMenu
            secondaryContextMenuActive = True
            AddHandler ctsm.SecondaryContextMenu.Closed, AddressOf SecondaryContextMenu_Closed
            ctsm.SecondaryContextMenu.Show(Cursor.Position)
            Return True
        End Function
        Private Sub SecondaryContextMenu_Closed(ByVal sender As Object, ByVal e As ToolStripDropDownClosedEventArgs)
            RemoveHandler DirectCast(sender, ContextMenuStrip).Closed, AddressOf SecondaryContextMenu_Closed
            lastShownSecondaryContextMenu = Nothing
            secondaryContextMenuActive = False
            Focus()
        End Sub
        Protected Overloads Overrides Sub OnClosing(ByVal e As ToolStripDropDownClosingEventArgs)
            If secondaryContextMenuActive Then
                e.Cancel = True
            End If
            MyBase.OnClosing(e)
        End Sub
    End Class
    Public Sub New()
        InitializeComponent()
        Dim itemPrimary1 As New CustomToolStripMenuItem("item primary 1")
        itemPrimary1.SecondaryContextMenu = New ContextMenuStrip()
        itemPrimary1.SecondaryContextMenu.Items.AddRange(New ToolStripMenuItem() {New ToolStripMenuItem("item primary 1.1"), New ToolStripMenuItem("item primary 1.2")})
        Dim itemPrimary2 As New CustomToolStripMenuItem("item primary 2")
        itemPrimary2.DropDownItems.Add("item primary 2, sub 1")
        itemPrimary2.DropDownItems.Add("item primary 2, sub 2")
        itemPrimary2.SecondaryContextMenu = New ContextMenuStrip()
        itemPrimary2.SecondaryContextMenu.Items.AddRange(New ToolStripMenuItem() {New ToolStripMenuItem("item primary 2.1"), New ToolStripMenuItem("item primary 2.2")})
        Dim primaryContextMenu As New CustomContextMenuStrip()
        primaryContextMenu.Items.AddRange(New ToolStripItem() {itemPrimary1, itemPrimary2})
        Me.ContextMenuStrip = primaryContextMenu
    End Sub

End Class

Sorry but i didnt understand anythng..Can u plz help me out?/

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.