首页 > 解决方案 > 如何让发件人在 Powershell 表单中使用鼠标右键单击菜单

问题描述

我使用动态标签。我需要知道在哪个动态标签上按下了右键。当我双击服务器名称时,它会写下我按下的标签。但是我怎样才能得到我按下的标签ContextMenuStrip

这是我的脚本的相关部分:

$contextMenuStrip1 = New-Object System.Windows.Forms.ContextMenuStrip
$Menu1a = $contextMenuStrip1.Items.Add("Item 1")
$Menu1a.add_Click(
        {   
        param($Sender,$EventArgs) write-host $Sender.name
        Write-Host $this.Text
        })
$Menu1b = $contextMenuStrip1.Items.Add("Item 2")
$Menu1b.add_Click({Write-Host $this.Text})


$Global:DynLblElemCol1 = foreach($Server in $Servers)
        {
            $Col = New-Object System.Windows.Forms.Label        
            $Col.Size = New-Object System.Drawing.Size(100, 18)
            $col.TextAlign = "MiddleCenter"
            $col.Text = $Server
            $Col.Name = $Server
            $Col.Add_DoubleClick({param($Sender,$EventArgs) write-host $Sender.Text})
            $Col.ContextMenuStrip = $contextMenuStrip1
            $Col
        }
    foreach($item in $DynLblElemCol1)
        {
            $item.Location = New-Object System.Drawing.Point(80, $CheckBoxCounter)
            $Form1.Controls.Add($item)
            $CheckBoxCounter = $item.Bottom + $LineSpace  # increment our counter
        }
}

标签: powershell

解决方案


推荐阅读