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:-
Ive done a search on google, but cannot find any relevant information to this, any ideas?
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
Ive done a search on google, but cannot find any relevant information to this, any ideas?
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
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
Works fine for me. Are you calling the AddLabel function appropriately?
e.g.
e.g.
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
addLabel(5, "Add", "new")
End Sub
Is addlabel a sub on the form? If not, you need to tell it which form you are adding the controls to.
ASKER
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
If I comment out out the MouseDown handler it works fine.
Now Im stuck for ideas how to get them both :-S
ASKER
My entire code is:
Its not really a programming issue now, more of a logic issue :-S
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
Its not really a programming issue now, more of a logic issue :-S
ASKER
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?
Is it possible to fire the menu on right mouse click, and start drag on left mouse click?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
e.g.
Open in new window