0

我有 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

前几天我收到了很棒的帮助,将变量传递给 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 打开事件确定发送者

4

1 回答 1

2

好的,您Labels在一个表单上有多个,并且它们都使用相同的ContextMenuStrip(所有Labels 的ContextMenuStrip属性都设置为同一个ContextMenuStrip控件)。

当用户右键单击 aLabel并选择一个菜单项时,您希望该菜单项的Text更改为单击LabelText

您可以使用您的ContextMenuStrip ItemClicked事件处理程序来执行此操作。使用处理程序的发送者和ToolStripItemClickedEventArgs参数来获取Label's和对单击Text的引用。ToolStripItem

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
于 2013-08-24T05:24:14.683 回答