PowerShell .NET 收集所有选中的节点

PowerShell .NET Collect all checked nodes

我几乎完成了 treeview 中 select 的小型 GUI。剩下的唯一障碍是收集所有前面有复选标记的节点。我在单击 'ButtonOK' 后尝试执行此操作,但我的 Foreach 并没有真正起作用。

代码:

$ButtonOK_Click = {
    foreach ($n in $treeView.Nodes) {
        if ($n.checked) {
            Write-Output $n.Name
        }
    }
}

$form = New-Object System.Windows.Forms.Form
$Form.ClientSize = '342, 502'
$Form.FormBorderStyle = 'FixedDialog'

$treeView = New-Object System.Windows.Forms.TreeView
$treeView.Dock = 'Fill'
$treeView.CheckBoxes = $true

$N1 = $treeView.Nodes.Add('Node 1') 
$N2 = $treeView.Nodes.Add('Node 2')
$N3 = $treeView.Nodes.Add('Node 3')

$newNode = New-Object System.Windows.Forms.TreeNode  
$newNode.Name = 'Sub 1'
$newNode.Text = 'Sub 1'
$N1.Nodes.Add($newNode) | Out-Null

$newNode = New-Object System.Windows.Forms.TreeNode  
$newNode.Name = 'Node 1 Sub 2'
$newNode.Text = 'Node 1 Sub 2'
$N1.Nodes.Add($newNode) | Out-Null #>

$ButtonOK = New-Object System.Windows.Forms.Button
$ButtonOK.DialogResult = 'OK'
$ButtonOK.Location = '245,467'
$ButtonOK.Size = '75,23'
$ButtonOK.Name = 'ButtonOK'
$ButtonOK.Text = 'OK'
$ButtonOK.add_Click($ButtonOK_Click)
$form.Controls.Add($ButtonOK)

$form.Controls.Add($treeView)
$form.ShowDialog()

感谢您的帮助。

在 C# 中有一个很好的例子 here

这是 Powershell 中的翻译:

function GetCheckedNode
{
    param($nodes)

    foreach ($n in $Nodes) {
        if ($n.nodes.count -gt 0)
        {
            GetCheckedNode $n.nodes
        }
        if ($n.checked) {
            Write-Host $n.Text
        }           
    }   
}
$ButtonOK_Click = {
    GetCheckedNode $treeView.Nodes
}

$form = New-Object System.Windows.Forms.Form
$Form.ClientSize = '342, 502'
$Form.FormBorderStyle = 'FixedDialog'

$treeView = New-Object System.Windows.Forms.TreeView
$treeView.Dock = 'Fill'
$treeView.CheckBoxes = $true

$N1 = $treeView.Nodes.Add('Node 1') 
$N2 = $treeView.Nodes.Add('Node 2')
$N3 = $treeView.Nodes.Add('Node 3')

$newNode = New-Object System.Windows.Forms.TreeNode  
$newNode.Name = 'Sub 1'
$newNode.Text = 'Sub 1'
$N1.Nodes.Add($newNode) | Out-Null

$newNode = New-Object System.Windows.Forms.TreeNode  
$newNode.Name = 'Node 1 Sub 2'
$newNode.Text = 'Node 1 Sub 2'
$N1.Nodes.Add($newNode) | Out-Null #>

$ButtonOK = New-Object System.Windows.Forms.Button
$ButtonOK.DialogResult = 'OK'
$ButtonOK.Location = '245,467'
$ButtonOK.Size = '75,23'
$ButtonOK.Name = 'ButtonOK'
$ButtonOK.Text = 'OK'
$ButtonOK.add_Click($ButtonOK_Click)
$form.Controls.Add($ButtonOK)

$form.Controls.Add($treeView)
$form.ShowDialog()

出于某种原因,我无法访问 Name 属性,但是 Text 属性 确实有效。您需要从按钮单击处理程序调用一个函数,该函数将递归自身以获取所有节点。完整代码如下:

Add-Type -AssemblyName System.Windows.Forms

function findChecked($node) {
  foreach ($n in $node.nodes) {
    if ($n.checked) { write-host $n.Text }
    findChecked($n)
  }
}

$form = New-Object System.Windows.Forms.Form
$Form.ClientSize = '342, 502'
$Form.FormBorderStyle = 'FixedDialog'

$treeView = New-Object System.Windows.Forms.TreeView
$treeView.Dock = 'Fill'
$treeView.CheckBoxes = $true

$N1 = $treeView.Nodes.Add('Node 1') 
$N2 = $treeView.Nodes.Add('Node 2')
$N3 = $treeView.Nodes.Add('Node 3')

$newNode = New-Object System.Windows.Forms.TreeNode  
$newNode.Name = 'Sub 1'
$newNode.Text = 'Sub 1'
$N1.Nodes.Add($newNode) | Out-Null

$newNode = New-Object System.Windows.Forms.TreeNode  
$newNode.Name = 'Node 1 Sub 2'
$newNode.Text = 'Node 1 Sub 2'
$N1.Nodes.Add($newNode) | Out-Null #>

$ButtonOK_Click = {
    findChecked($treeView)
}

$ButtonOK = New-Object System.Windows.Forms.Button
$ButtonOK.DialogResult = 'OK'
$ButtonOK.Location = New-Object System.Drawing.Point(245,467)
$ButtonOK.Size = New-Object System.Drawing.Size(75,23) 
$ButtonOK.Name = 'ButtonOK'
$ButtonOK.Text = 'OK'
$ButtonOK.UseVisualStyleBackColor = $True
$ButtonOK.add_Click($ButtonOK_Click)
$form.Controls.Add($ButtonOK)

$form.Controls.Add($treeView)
$form.ShowDialog()