Reputation: 293
I have 30 labels. They can have any value I want.
I need to be able to assign one context menu to them all then determine which label was clicked in order to use my x
variable.
Private Sub Label_Click(sender As Object, e As MouseEventArgs) Handles Label1.MouseClick, Label2.MouseClick, Label3.MouseClick, Label4.MouseClick, _
Label5.MouseClick, Label6.MouseClick, Label7.MouseClick, Label8.MouseClick, Label9.MouseClick, Label10.MouseClick, Label11.MouseClick, _
Label12.MouseClick, Label13.MouseClick, Label14.MouseClick, Label15.MouseClick, Label15.MouseClick, Label16.MouseClick, Label17.MouseClick, _
Label18.MouseClick, Label19.MouseClick, Label20.MouseClick, Label21.MouseClick, Label22.MouseClick, Label23.MouseClick, Label24.MouseClick, _
Label25.MouseClick, Label26.MouseClick, Label27.MouseClick, Label28.MouseClick, Label29.MouseClick, Label30.MouseClick
Dim x As String = sender.Text
xmlinteraction.appCall(x)
End Sub
I received awesome help the other day passing variable into contextmenustrip But I am too new to put it all together and make it work. I understand what we are trying to do, but not all the syntax. Please help.
Jay, Here is what I put together from the code you gave me. Is this what you were thinking? I feel like I missing something still and further clean the code. Possibly removing the case statements.
Private Sub rcmenuOption(x, y)
' x is equal to what the menu item was clicked
' Create case stament for that to call the correct xmlinteraction passing in y
Select Case x
Case "Copy Link"
copyClipboard(y)
End Select
End Sub
Private Sub rcmenuClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ToolStripItemClickedEventArgs) Handles rcmenu.ItemClicked
' Get the Label clicked from the SourceControl property of the clicked ContextMenuStrip.
Dim contextMenu = DirectCast(sender, ContextMenuStrip)
Dim label = DirectCast(contextMenu.SourceControl, Label)
Dim var2 As String = label.Text
' Get the clicked menu strip and update its Text to the Label's Text.
Dim toolStripItem = e.ClickedItem
Dim var As String = toolStripItem.Text
rcmenuOption(var, var2)
End Sub
contextmenustrip opening event determining sender
Upvotes: 0
Views: 3886
Reputation: 53593
OK, you have a number of Labels
on a form and all of them use the same ContextMenuStrip
(all the Label
s have their ContextMenuStrip
property set to the same ContextMenuStrip
control).
When the user right clicks a Label
and selects a menu item, you want that menu item's Text
to change to the clicked Label
's Text
.
You can do this using your ContextMenuStrip
ItemClicked
event handler. Use the handler's sender and ToolStripItemClickedEventArgs
parameters to get the Label
's Text
and a reference to the ToolStripItem
clicked.
Private Sub ContextMenuStrip1_ItemClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ToolStripItemClickedEventArgs) Handles ContextMenuStrip1.ItemClicked
' Get the Label clicked from the SourceControl property of the clicked ContextMenuStrip.
Dim contextMenu = DirectCast(sender, ContextMenuStrip)
Dim label = DirectCast(contextMenu.SourceControl, Label)
' Get the clicked menu strip and update its Text to the Label's Text.
Dim toolStripItem = e.ClickedItem
toolStripItem.Text = label.Text
End Sub
Upvotes: 2