contextmenustrip开放事件确定发送者

时间:2013-08-24 05:05:25

标签: vb.net winforms

我有30个标签。他们可以拥有我想要的任何价值。 我需要能够为它们分配一个上下文菜单,然后确定单击了哪个标签才能使用我的x变量。

    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

前几天我收到了很棒的帮助passing variable into contextmenustrip 但我太新了,无法将它们整合在一起并使其发挥作用。我理解我们要做的是什么,但不是所有的语法。请帮忙。


周杰伦 这是我从你给我的代码中汇总的内容。这是你在想什么?我觉得我遗漏了一些东西并进一步清理代码。可能会删除案例陈述。

   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

1 个答案:

答案 0 :(得分:2)

好的,表单上有一些Labels,并且所有ContextMenuStrip都使用相同的Label(所有ContextMenuStrip都设置了ContextMenuStrip属性相同的Label控件)。

当用户右键单击Text并选择菜单项时,您希望该菜单项的Label更改为所点击的Text的{​​{1}}。

您可以使用ContextMenuStrip ItemClicked事件处理程序执行此操作。使用处理程序的发件人和ToolStripItemClickedEventArgs参数获取Label的{​​{1}}以及对Text点击的引用。

ToolStripItem