双击listview =单击按钮

时间:2017-09-07 08:37:21

标签: winforms powershell-v3.0 listviewitem double-click

我在Powershell中有一个带有2个按钮的Windows窗体和一个包含2个列“Nomducollège”($ col1)和“Adresse IP”($ col2)的列表视图。

列表视图由遍历$ MaListe的函数“AddData”填充。

如果我在列表视图中选择了一个lign并单击“连接器”($ btnCo)按钮,则会出现第二个Windows窗体。

我正在寻找一种方法,当我双击listview的一行时,它会启动Connect按钮事件。

我自愿删除了Connect按钮事件,因为有许多行,但如果您希望我可以提供它。

这是我的代码:

Set-ExecutionPolicy RemoteSigned -Scope LocalMachine -Force
[reflection.assembly]::loadwithpartialname("System.Windows.Forms") | Out-Null
[reflection.assembly]::loadwithpartialname("System.Drawing") | Out-Null

function addUser
 {
 param([string]$IP,[string]$Nom)
 $d=New-Object PSObject
 $d | Add-Member -Name "IP" -MemberType NoteProperty -Value $IP
 $d | Add-Member -Name "Nom" -MemberType NoteProperty -Value $Nom
 return $d
 }
$MaListe=@()
$MaListe+=addUser -IP "172.20.143.241" -Nom "Albert Schweitzer"
$MaListe+=addUser -IP "172.20.195.241" -Nom "Anne Franck"
$MaListe+=addUser -IP "172.20.67.241" -Nom "Antoine Guichard"
$MaListe+=addUser -IP "172.20.167.241" -Nom "Aristide Briand"
$MaListe+=addUser -IP "172.20.63.241" -Nom "Bois de la Rive"
$MaListe+=addUser -IP "172.20.115.241" -Nom "Charles Exbrayat"
$MaListe+=addUser -IP "172.20.179.241" -Nom "Claude Fauriel"
$MaListe+=addUser -IP "172.20.91.241" -Nom "Emile Falabregue"
$MaListe+=addUser -IP "172.20.107.241" -Nom "Ennemond Richard"
$MaListe+=addUser -IP "172.20.119.241" -Nom "Francois Truffaut"
$MaListe+=addUser -IP "172.20.135.241" -Nom "Gambetta"
$MaListe+=addUser -IP "172.20.83.241" -Nom "Gaston Baty"
$MaListe+=addUser -IP "172.20.55.241" -Nom "Honore Urfe"
$MaListe+=addUser -IP "172.20.147.241" -Nom "Jacques Brel"
$MaListe+=addUser -IP "172.20.159.241" -Nom "Jacques Prevert"
$MaListe+=addUser -IP "172.20.191.241" -Nom "Jean Daste"
$MaListe+=addUser -IP "172.20.87.241" -Nom "Jean de la Fontaine"
$MaListe+=addUser -IP "172.20.139.241" -Nom "Jean Papon"
$MaListe+=addUser -IP "172.20.51.241" -Nom "Jean Rostand"
$MaListe+=addUser -IP "172.20.19.241" -Nom "Jules Ferry"
$MaListe+=addUser -IP "172.20.175.241" -Nom "Jules Romains"
$MaListe+=addUser -IP "172.20.35.241" -Nom "Jules Valles Ric"
$MaListe+=addUser -IP "172.20.127.241" -Nom "Jules Valles Sainte"
$MaListe+=addUser -IP "172.20.111.241" -Nom "LAstree"
$MaListe+=addUser -IP "172.20.199.241" -Nom "La Cote Roannaise "
$MaListe+=addUser -IP "172.20.99.241" -Nom "Le Breuil"
$MaListe+=addUser -IP "172.20.151.241" -Nom "Le Palais"
$MaListe+=addUser -IP "172.20.71.241" -Nom "Le Pilat"
$MaListe+=addUser -IP "172.20.183.241" -Nom "Le Portail Rouge"
$MaListe+=addUser -IP "172.20.131.241" -Nom "Leonard de Vinci"
$MaListe+=addUser -IP "172.20.27.241" -Nom "Les Bruneaux"
$MaListe+=addUser -IP "172.20.15.241" -Nom "Les Champs"
$MaListe+=addUser -IP "172.20.43.241" -Nom "Les Etines"
$MaListe+=addUser -IP "172.20.79.241" -Nom "Les Montagnes du Matin"
$MaListe+=addUser -IP "172.20.47.241" -Nom "Louis Aragon"
$MaListe+=addUser -IP "172.20.123.241" -Nom "Louis Gruner"
$MaListe+=addUser -IP "172.20.187.241" -Nom "Louise Michel"
$MaListe+=addUser -IP "172.20.59.241" -Nom "Marc Seguin"
$MaListe+=addUser -IP "172.20.11.241" -Nom "Mario Meunier"
$MaListe+=addUser -IP "172.20.203.241" -Nom "Massenet Fourneyron"
$MaListe+=addUser -IP "172.20.23.241" -Nom "Michel de Montaigne"
$MaListe+=addUser -IP "172.20.171.241" -Nom "Michel Servet"
$MaListe+=addUser -IP "172.20.163.241" -Nom "Nicolas Conte"
$MaListe+=addUser -IP "172.20.95.241" -Nom "Papire Masson"
$MaListe+=addUser -IP "172.20.39.241" -Nom "Pierre et Marie Curie"
$MaListe+=addUser -IP "172.20.155.241" -Nom "Pierre Joannon"
$MaListe+=addUser -IP "172.20.103.241" -Nom "Puits de la Loire"
$MaListe+=addUser -IP "172.20.75.241" -Nom "Robert Schuman"
$MaListe+=addUser -IP "172.20.31.241" -Nom "Waldeck Rousseau"

function AddData
{
    param($MaListe, $list)

    $list.BeginUpdate()
    $list.items.Clear() 
    foreach ($proc in $MaListe)
    {
        $nomcol = New-Object System.Windows.Forms.ListViewItem($proc.Nom)
        [Void] $list.Items.Add($nomcol)
        $ip = New-Object System.Windows.Forms.ListViewItem($proc.IP)
        [Void]$nomcol.SubItems.Add($proc.IP)
    }
    $list.EndUpdate()
}

$btnList_OnClick= 
{
    AddData $MaListe $list
}

$btnCo_OnClick= 
{

}


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 $list.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
$list.BeginUpdate()
$list.Items.Clear()
foreach($ListItem in $ListItems)
    {
    $list.Items.Add($ListItem[1])
    }
$list.EndUpdate()
}


# DEFINITION DE LA 1ERE FENETRE
$form = New-Object System.Windows.Forms.Form
$form.Size = New-Object System.Drawing.Size(420,1000)
$form.Text = "Wake On LAN"
$form.StartPosition = "CenterScreen"

# AJOUT D'UN BOUTON List POUR RAFRAICHIR LA LISTE DES COLLEGES PAR RAPPORT AU CSV
$btnList = New-Object System.Windows.Forms.Button
$btnList.TabIndex = 1
$btnList.Name = "btnList"
$btnList.Size = New-Object System.Drawing.Size(99,23)
$btnList.Text = "Rafraichir"
$btnList.Location = New-Object System.Drawing.Point(3,5)
$btnList.add_Click($btnList_OnClick)
$form.Controls.Add($btnList)

# AJOUT D'UN BOUTON List POUR SE CONNECTER EN SSH AU SERVEUR
# PRE-REQUIS : AVOIR INSTALLE LE MODULE POSH-SSH
$btnCo = New-Object System.Windows.Forms.Button
$btnCo.TabIndex = 2
$btnCo.Name = "btnCo"
$btnCo.Size = New-Object System.Drawing.Size(99,23) 
$btnCo.Text = "Connecter"
$btnCo.Location = New-Object System.Drawing.Point(105,5)
$btnCo.add_Click($btnCo_OnClick)
$form.Controls.Add($btnCo)

# TABLEAU COLLEGE - IP
$list = New-Object System.Windows.Forms.ListView
$list.TabIndex = 3
$list.Location = New-Object System.Drawing.Point(3,30)
$list.Size = New-Object System.Drawing.Size(390, 880)
$list.View = "Details"
$list.GridLines = $true
$list.FullRowSelect = $true
$list.Scrollable = $true
$form.Controls.Add($list)

# CREATION DES COLONNES
$col1 = $list.Columns.Add("Nom du collège", 230)
$col2 = $list.Columns.Add("Adresse IP", 150)

# AJOUT DES DONNEES DU CSV
AddData $MaListe $list

## ACTIVATION DU TRI AUTO SUR LE TABLEAU
$list.add_ColumnClick({SortListView $_.Column})

# DEMARRAGE DE LA FENETRE
$form.ShowDialog()| Out-Null

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

您需要将FullRowSelect的{​​{1}}属性设置为ListView并处理true事件。将您想要拥有的逻辑放在函数的ItemActivate事件中,并将Click ItemActivate事件放在一个函数中。

<强> 实施例

在以下示例中,通过双击ListView的项目或选择和项目并单击ListView,消息框将显示所选项目的文本:

Button