Link to home
Start Free TrialLog in
Avatar of tonelm54
tonelm54

asked on

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?
Avatar of kaufmed
kaufmed
Flag of United States of America image

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

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

Avatar of tonelm54
tonelm54

ASKER

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

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

Is addlabel a sub on the form?  If not, you need to tell it which form you are adding the controls to.
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
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
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?
ASKER CERTIFIED SOLUTION
Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial