Nada_ward 0 Junior Poster in Training

Hello
I have problem with context menu
when I right click on picture box , contextmenu appear for picturebox
when I click on label , the menue of picturebox appear again until I click right on the label another time

Public Class Form1
    Dim content As String = ""
    Sub mouse_up( _
               ByVal sender As System.Object, _
               ByVal e As System.Windows.Forms.MouseEventArgs)
        'Checking the Mouse right Button on the top
        '   Try

        
        ContextHandler(sender, e)
        sender.ContextMenu.Show(sender, New Point(e.X, e.Y))
        ' Catch ex As Exception
        '      MsgBox(ex.ToString)
        '  End Try
    End Sub
    Protected Sub ContextHandler(ByVal sender As System.Object, _
       ByVal e As System.EventArgs)
        ' Clear all previously added MenuItems.
        menu1.MenuItems.Clear()
        '  MsgBox(sender.GetType.ToString)
        If sender.GetType.ToString.Equals("System.Windows.Forms.Label") Then
            'get text of label
            content = CType(sender, Label).Text
            AddContextMenu("label")

            AddContextMenu("picture", False)
           

        Else


            AddContextMenu("label", False)
            AddContextMenu("picture", False)

           
        End If
    End Sub
    Private Sub AddContextMenu(ByVal Caption As String, Optional ByVal IsEnabled As Boolean = True)

        Dim objMenuItem As MenuItem

        objMenuItem = New MenuItem
        objMenuItem.Text = Caption
        objMenuItem.Enabled = IsEnabled
        Menu1.MenuItems.Add(objMenuItem)
        ' Handle event that happen when I click menu items
        AddHandler objMenuItem.Click, AddressOf MenuClick

    End Sub
    Private Sub MenuClick(ByVal sender As Object, ByVal e As System.EventArgs)

        'Point to menu item clicked
        Dim objCurMenuItem As MenuItem = CType(sender, MenuItem)
        

        'create the submenu based on whatever selection is being made, mnuSelection
        Select Case objCurMenuItem.Text

            Case "label"
                MsgBox("label enabled")

            Case "ppicture"
                MsgBox("picture enabled")

        End Select

        objCurMenuItem = Nothing


    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        AddHandler Label1.MouseUp, AddressOf mouse_up
        AddHandler PictureBox1.MouseUp, AddressOf mouse_up
    End Sub
End Class
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.