Winforms Powershell winform ListView鼠标右键关联菜单绑定到特定ListView项

Winforms Powershell winform ListView鼠标右键关联菜单绑定到特定ListView项,winforms,powershell,listview,contextmenu,listitem,Winforms,Powershell,Listview,Contextmenu,Listitem,我正在尝试为listview中的每个项目创建鼠标右键单击,以便能够在远程pc上执行命令。我只是不知道如何将单独的上下文菜单分配给特定的ListViewItem。 我也尝试过用数组创建表单,但我遇到了同样的问题,我不知道如何在互联网上搜索表单。 谢谢你的帮助! 代码如下: ## Set up the environment Add-Type -AssemblyName System.Windows.Forms $LastColumnClicked = 0 # tracks the last co

我正在尝试为listview中的每个项目创建鼠标右键单击,以便能够在远程pc上执行命令。我只是不知道如何将单独的上下文菜单分配给特定的ListViewItem。 我也尝试过用数组创建表单,但我遇到了同样的问题,我不知道如何在互联网上搜索表单。 谢谢你的帮助! 代码如下:


## Set up the environment
Add-Type -AssemblyName System.Windows.Forms
$LastColumnClicked = 0 # tracks the last column number that was clicked
$LastColumnAscending = $false # tracks the direction of the last sort of this column
 
## Create a form and a ListView
$Form = New-Object System.Windows.Forms.Form
$ListView = New-Object System.Windows.Forms.ListView
 
## Configure the form
$Form.Text = "Computer List"
 
## Configure the ListView
$ListView.View = [System.Windows.Forms.View]::Details
$ListView.Width = $Form.ClientRectangle.Width
$ListView.Height = $Form.ClientRectangle.Height
$ListView.Anchor = "Top, Left, Right, Bottom"
 
# Add the ListView to the Form
$Form.Controls.Add($ListView)
 
# Add columns to the ListView
$ListView.Columns.Add("Computer Name", -2) | Out-Null
$ListView.Columns.Add(" Test") | Out-Null
 
# Add list items
$contextMenuStrip = [System.Windows.Forms.ContextMenuStrip]@{}
$listView.Font = 'Microsoft Sans Serif,10'
##Computer1##
$ListViewItem = New-Object System.Windows.Forms.ListViewItem("computer1")
$click1 = {Invoke-Command -UseSSL -ComputerName computer1 }
$click2 = {Invoke-Command -UseSSL -ComputerName computer1 }
$item1 = $contextMenuStrip.Items.Add("Run 1")
$item1.add_Click($click1)
$item2 = $contextMenuStrip.Items.Add("Run 2")
$item2.add_Click($click2)
$listView.Add_MouseClick({param($sender,$e)
    if ($e.Button -eq [System.Windows.Forms.MouseButtons]::Right){
        if ($listView.FocusedItem.GetBounds(
            [System.Windows.Forms.ItemBoundsPortion]::Entire).Contains($e.Location)){
            $contextMenuStrip.Show([System.Windows.Forms.Cursor]::Position)
        }
    } 
})
$ListView.Items.Add($ListViewItem) | Out-Null

##computer2##
$contextMenuStrip = [System.Windows.Forms.ContextMenuStrip]@{}
$ListViewItem = New-Object System.Windows.Forms.ListViewItem("computer2")
$click1 = {Invoke-Command -UseSSL -ComputerName computer2 }
$click2 = {Invoke-Command -UseSSL -ComputerName computer2 }
$item1 = $contextMenuStrip.Items.Add("Run 3")
$item1.add_Click($click1)
$item2 = $contextMenuStrip.Items.Add("Run 4")
$item2.add_Click($click2)
$listView.Add_MouseClick({param($sender,$e)
    if ($e.Button -eq [System.Windows.Forms.MouseButtons]::Right){
        if ($listView.FocusedItem.GetBounds(
            [System.Windows.Forms.ItemBoundsPortion]::Entire).Contains($e.Location)){
            $contextMenuStrip.Show([System.Windows.Forms.Cursor]::Position)
        }
    } 
})
$ListView.Items.Add($ListViewItem) | Out-Null

## Set up the event handler
$ListView.add_ColumnClick({SortListView $_.Column})
 
## Event handler
function SortListView
{
 param([parameter(Position=0)][UInt32]$Column)
 
$Numeric = $true # determine how to sort
 
# if the user clicked the same column that was clicked last time, reverse its sort order. otherwise, reset for normal ascending sort
if($Script:LastColumnClicked -eq $Column)
{
    $Script:LastColumnAscending = -not $Script:LastColumnAscending
}
else
{
    $Script:LastColumnAscending = $true
}
$Script:LastColumnClicked = $Column
$ListItems = @(@(@())) # three-dimensional array; column 1 indexes the other columns, column 2 is the value to be sorted on, and column 3 is the System.Windows.Forms.ListViewItem object
 
foreach($ListItem in $ListView.Items)
{
    # if all items are numeric, can use a numeric sort
    if($Numeric -ne $false) # nothing can set this back to true, so don't process unnecessarily
    {
        try
        {
            $Test = [Double]$ListItem.SubItems[[int]$Column].Text
        }
        catch
        {
            $Numeric = $false # a non-numeric item was found, so sort will occur as a string
        }
    }
    $ListItems += ,@($ListItem.SubItems[[int]$Column].Text,$ListItem)
}
 
# create the expression that will be evaluated for sorting
$EvalExpression = {
    if($Numeric)
    { return [Double]$_[0] }
    else
    { return [String]$_[0] }
}
 
# all information is gathered; perform the sort
$ListItems = $ListItems | Sort-Object -Property @{Expression=$EvalExpression; Ascending=$Script:LastColumnAscending}
 
## the list is sorted; display it in the listview
$ListView.BeginUpdate()
$ListView.Items.Clear()
foreach($ListItem in $ListItems)
{
    $ListView.Items.Add($ListItem[1])
}
$ListView.EndUpdate()
}
 
## Show the form
$Response = $Form.ShowDialog()

})
$ListView.Items.Add($ListViewItem) | Out-Null

这实际上不是PowerShell代码问题,。这是一个用户体验/用户界面设计问题。此LINE[$ListView.Items.Add($ListViewItem)| Out Null]必须高于[$Response=$Form.ShowDialog()],否则在表单关闭时会出错,并且此“}”)与您发布的内容不匹配。所以,也许把这个移到代码审查,并在那里发布更多。您是在不首先选择图元还是在选择图元之后尝试执行此操作?在普通的UX/UI模型设计中,不能在未选择的元素上呈现动作。通过这种方式,您可以使用所选的。此外,如果您一次单步通过代码段,则会出现需要解决的其他错误。例如[使用“1”参数调用“Add”时出现异常):“无法在多个位置添加或插入项“computer2”。必须首先将其从当前位置删除或克隆。第149行的参数名称:item:1]