gpt4 book ai didi

.net - TreeNodes的PowerShell .NET Mimic Windows默认复选框行为

转载 作者:行者123 更新时间:2023-12-02 23:28:01 26 4
gpt4 key购买 nike

Windows.Forms表单中,我们正在尝试实现以下目标:

  • 选中/取消选中父节点时,选中/取消选中所有子节点。
  • 自动检查父节点是否已检查其所有子节点。
  • 如果未选中父节点的子节点之一,则自动取消选中父节点。

  • 测试代码:
    $form = New-Object System.Windows.Forms.Form

    $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 = 'Node 1 Sub 1'
    $newNode.Text = 'Node 1 Sub 1'
    $N1.Nodes.Add($newNode)

    $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
    # There can be deeper levels of treenodes

    $TV_AfterCheck = {
    if($_.Node.Checked) {
    #[System.Windows.Forms.MessageBox]::Show('Node ' + $_.Node.Text + ' checked')
    }
    else {
    #[System.Windows.Forms.MessageBox]::Show('Node ' + $_.Node.Text + ' unchecked')
    }
    }

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

    我发现的解决方案是针对另一种语言量身定制的,我无法在PowerShell中使用它。关于此主题的信息很多,但是都是其他编程语言提供的信息。

    其他语言的解决方案:
    Private Sub CheckChildNode(ByVal currNode As TreeNode)
    'set the children check status to the same as the current node
    Dim checkStatus As Boolean = currNode.Checked
    For Each node As TreeNode In currNode.Nodes
    node.Checked = checkStatus
    CheckChildNode(node)
    Next
    End Sub

    Private Sub CheckParentNode(ByVal currNode As TreeNode)
    Dim parentNode As TreeNode = currNode.Parent
    If parentNode Is Nothing Then Exit Sub
    parentNode.Checked = True
    For Each node As TreeNode In parentNode.Nodes
    If Not node.Checked Then
    parentNode.Checked = False
    Exit For
    End If
    Next
    CheckParentNode(parentNode)
    End Sub

    Private Sub treeview_AfterCheck(ByVal sender As System.Object, ByVal e As
    System.Windows.Forms.TreeViewEventArgs) Handles treeview.AfterCheck
    RemoveHandler treeview.AfterCheck, AddressOf treeview_AfterCheck
    CheckChildNode(e.Node)
    CheckParentNode(e.Node)
    AddHandler treeview.AfterCheck, AddressOf treeview_AfterCheck
    End Sub

    如果有人可以帮助我将其粘贴在一起,使其在PowerShell中正常工作,那将是非常不错的。谢谢您的帮助。

    最佳答案

    下面基本上是对您包括的VB.NET代码的重写:

    您需要包括以下功能:

    function checkChildNode($node) {
    $checkStatus = $node.checked
    foreach ($n in $node.nodes) {
    $n.checked = $checkstatus
    checkChildNode($n)
    }
    }

    function checkParentNode($node) {
    $parent = $node.parent
    if($parent -eq $null) {
    return
    }
    $parent.checked = $true
    foreach ($n in $parent.nodes) {
    if(!$n.checked) {
    $parent.checked = $false
    break
    }
    }
    checkparentNode($parent)
    }

    并将您的事件处理程序更改为:
    $TV_AfterCheck = {
    $treeView.Remove_AfterCheck($TV_AfterCheck)
    checkChildNode($_.node)
    checkParentNode($_.node)
    $treeView.Add_AfterCheck($TV_AfterCheck)
    }

    这将使您拥有所要遵循的行为。

    关于.net - TreeNodes的PowerShell .NET Mimic Windows默认复选框行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27900278/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com