ContextMenuStrip which item has been selected

Ive built some simple code to add a label onto a form and attach a ContextMenu to it, however I cannot seem to find a way to discover what was pressed (ie New, Edit or delete). Ive looked through the values of sender, and although I can find what label was clicked, I cant seem to find what item was clicked.

What Ive got soo far:-
Public Class Form1

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim newLabel As New Label
        newLabel.Size = New Size(32, 16)
        newLabel.Location = New Point(10, 10)
        newLabel.Text = "Hello"

        Dim newContextMenu As New ContextMenuStrip
        newContextMenu.Items.Add("New")
        newContextMenu.Items.Add("Edit")
        newContextMenu.Items.Add("Delete")

        AddHandler newContextMenu.Click, AddressOf menuClicked


        newLabel.ContextMenuStrip = newContextMenu

        Me.Controls.Add(newLabel)
    End Sub

    Private Sub menuClicked(ByVal sender As Object, ByVal e As EventArgs)
        'Get what label has been clicked
        Me.Text = CType(sender, ContextMenuStrip).SourceControl.Text

        'Get which menu item was clicked

    End Sub

End Class

Open in new window


Ive done a search on google, but cannot find any relevant information to this, any ideas?
tonelm54Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

käµfm³d 👽Commented:
Is it possible (for your design) to add a handler to the ToolStripItem's Click event, rather than adding it to the ContextMenuStrip itself?

e.g.

Public Class Form1

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim newLabel As New Label
        newLabel.Size = New Size(32, 16)
        newLabel.Location = New Point(10, 10)
        newLabel.Text = "Hello"

        Dim newContextMenu As New ContextMenuStrip
        Dim tsNew As ToolStripItem = newContextMenu.Items.Add("New")
        Dim tsEdit As ToolStripItem = newContextMenu.Items.Add("Edit")
        Dim tsDelete As ToolStripItem = newContextMenu.Items.Add("Delete")

        'AddHandler newContextMenu.Click, AddressOf menuClicked
        AddHandler tsNew.Click, AddressOf menuClicked
        AddHandler tsEdit.Click, AddressOf menuClicked
        AddHandler tsDelete.Click, AddressOf menuClicked


        newLabel.ContextMenuStrip = newContextMenu

        Me.Controls.Add(newLabel)
    End Sub

    Private Sub menuClicked(ByVal sender As Object, ByVal e As EventArgs)
        'Get what label has been clicked
        'Me.Text = CType(sender, ContextMenuStrip).SourceControl.Text
        Me.Text = CType(sender, ToolStripItem).Text

        'Get which menu item was clicked

    End Sub

End Class

Open in new window

0
lluddenCommented:
Add an event handler to each item in the menu.  Can be a Different one for each or one that handles them all.

Public Class Form1

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim newLabel As New Label
        newLabel.Size = New Size(32, 16)
        newLabel.Location = New Point(50, 50)
        newLabel.Text = "Hello"
        Dim newContextMenu As New ContextMenuStrip
        Dim CMS As ToolStripMenuItem
        CMS = newContextMenu.Items.Add("New")
        AddHandler CMS.Click, AddressOf MenuStrip_Click
        CMS = newContextMenu.Items.Add("Edit")
        AddHandler CMS.Click, AddressOf MenuStrip_Click
        CMS = newContextMenu.Items.Add("Delete")
        AddHandler CMS.Click, AddressOf MenuStrip_Click

        newLabel.ContextMenuStrip = newContextMenu

        Me.Controls.Add(newLabel)
    End Sub

    Private Sub MenuStrip_Click(sender As System.Object, e As System.EventArgs)
        Dim itm As ToolStripMenuItem = CType(sender, ToolStripMenuItem)
        Select Case itm.Text
            Case "New"
                MsgBox("New")
            Case "Edit"
                MsgBox("Edit")
            Case "Delete"
                MsgBox("Delete")
        End Select
    End Sub
End Class

Open in new window

0
tonelm54Author Commented:
Not sure why, but it works on its own, but not when I copy it into my own sub:-
    Sub addLabel(ByVal x As Integer, ByVal lblTitle As String, ByVal lblText As String)
        Dim lblTest As New Label

        lblTest.Size = New Size(60, 13)
        lblTest.Text = lblTitle
        lblTest.Top = x * 15
        lblTest.Left = 10

        Dim ttTitle As New ToolTip
        ttTitle.ToolTipTitle = lblTitle
        ttTitle.SetToolTip(lblTest, lblText)
        ttTitle.UseFading = True

        Dim newContextMenu As New ContextMenuStrip
        Dim CMS As ToolStripMenuItem
        CMS = newContextMenu.Items.Add("New")
        AddHandler CMS.Click, AddressOf menuClicked
        CMS = newContextMenu.Items.Add("Edit")
        AddHandler CMS.Click, AddressOf menuClicked
        CMS = newContextMenu.Items.Add("Delete")
        AddHandler CMS.Click, AddressOf menuClicked

        lblTest.ContextMenuStrip = newContextMenu

        lblTest.Tag = New Dictionary(Of String, String)
        Dim dict As Dictionary(Of String, String) = CType(lblTest.Tag, Dictionary(Of String, String))
        dict.Add("Text", lblText)

        AddHandler lblTest.DragEnter, AddressOf lblDragEnter
        AddHandler lblTest.MouseDown, AddressOf lblMouseDown

        Me.Controls.Add(lblTest)
    End Sub

    Private Sub menuClicked(ByVal sender As Object, ByVal e As EventArgs)
        Dim itm As ToolStripMenuItem = CType(sender, ToolStripMenuItem)
        Select Case itm.Text
            Case "New"
                MsgBox("New")
            Case "Edit"
                MsgBox("Edit")
            Case "Delete"
                MsgBox("Delete")
        End Select
    End Sub

Open in new window

0
Learn Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

käµfm³d 👽Commented:
Works fine for me. Are you calling the AddLabel function appropriately?

e.g.

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    addLabel(5, "Add", "new")
End Sub

Open in new window

0
lluddenCommented:
Is addlabel a sub on the form?  If not, you need to tell it which form you are adding the controls to.
0
tonelm54Author Commented:
Ahh, found the issue, its because I have MouseDown and a context menu.

If I comment out out the MouseDown handler it works fine.

Now Im stuck for ideas how to get them both :-S
0
tonelm54Author Commented:
My entire code is:
Public Class Form1

    Private Sub lblDragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs)
        If e.Data.GetDataPresent(DataFormats.Text) Then
            e.Effect = DragDropEffects.Copy
        Else
            e.Effect = DragDropEffects.None
        End If
    End Sub
    Private Sub lblMouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
        Dim c As String = CType(sender.Tag, Dictionary(Of String, String))("Text").ToString

        CType(sender, Label).DoDragDrop(c, DragDropEffects.Copy)
    End Sub

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        For x = 1 To 10 Step 1
            addLabel(x, "Title " & x, "First Line" & vbCr & "Second line")
        Next
    End Sub

    Sub addLabel(ByVal x As Integer, ByVal lblTitle As String, ByVal lblText As String)
        Dim lblTest As New Label

        lblTest.Size = New Size(60, 13)
        lblTest.Text = lblTitle
        lblTest.Top = x * 15
        lblTest.Left = 10

        Dim ttTitle As New ToolTip
        ttTitle.ToolTipTitle = lblTitle
        ttTitle.SetToolTip(lblTest, lblText)


        AddHandler lblTest.DragEnter, AddressOf lblDragEnter
        AddHandler lblTest.MouseDown, AddressOf lblMouseDown

        Dim newContextMenu As New ContextMenuStrip
        Dim CMS As ToolStripMenuItem
        CMS = newContextMenu.Items.Add("New")
        AddHandler CMS.Click, AddressOf menuClicked
        CMS = newContextMenu.Items.Add("Edit")
        AddHandler CMS.Click, AddressOf menuClicked
        CMS = newContextMenu.Items.Add("Delete")
        AddHandler CMS.Click, AddressOf menuClicked

        lblTest.ContextMenuStrip = newContextMenu

        lblTest.Tag = New Dictionary(Of String, String)
        Dim dict As Dictionary(Of String, String) = CType(lblTest.Tag, Dictionary(Of String, String))
        dict.Add("Text", lblText)


        Me.Controls.Add(lblTest)
    End Sub

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

    Private Sub menuClicked(ByVal sender As Object, ByVal e As EventArgs)
        Dim itm As ToolStripMenuItem = CType(sender, ToolStripMenuItem)
        Select Case itm.Text
            Case "New"
                MsgBox("New")
            Case "Edit"
                MsgBox("Edit")
            Case "Delete"
                MsgBox("Delete")
        End Select
    End Sub
End Class

Open in new window


Its not really a programming issue now, more of a logic issue :-S
0
tonelm54Author Commented:
Ok, thought of a solution but no idea how to implement it.

Is it possible to fire the menu on right mouse click, and start drag on left mouse click?
0
CodeCruiserCommented:
>Is it possible to fire the menu on right mouse click, and start drag on left mouse click?

Change this

Private Sub lblMouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
        Dim c As String = CType(sender.Tag, Dictionary(Of String, String))("Text").ToString

        CType(sender, Label).DoDragDrop(c, DragDropEffects.Copy)
    End Sub

to

Private Sub lblMouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
    if e.Button = MouseButtons.Left Then
        Dim c As String = CType(sender.Tag, Dictionary(Of String, String))("Text").ToString

        CType(sender, Label).DoDragDrop(c, DragDropEffects.Copy)
    End If
    End Sub
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.