Powershell Winform ListView鼠标右键上下文菜单绑定到特定的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

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...