The code below works as deigned. No matter how fast I execute doubleclick, I will always have child nodes checked or unchecked and the checkbox of the parent not will reflect the state of child nodes:
Code: Select all
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
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
checkparentNode($parent)
}
$form = New-Object System.Windows.Forms.Form
$treeView = New-Object System.Windows.Forms.TreeView
$treeView.Dock = 'Fill'
$treeView.CheckBoxes = $true
$N1 = $treeView.Nodes.Add('Category 1')
$N2 = $treeView.Nodes.Add('Category 2')
$N3 = $treeView.Nodes.Add('Category 3')
$newNode = New-Object System.Windows.Forms.TreeNode
$newNode.Name = 'Test-A'
$newNode.Text = 'Test-A'
$N1.Nodes.Add($newNode)
$newNode = New-Object System.Windows.Forms.TreeNode
$newNode.Name = 'Test-B'
$newNode.Text = 'Test-B'
$N1.Nodes.Add($newNode)
$newNode = New-Object System.Windows.Forms.TreeNode
$newNode.Name = 'Component 0'
$newNode.Text = 'Component 0'
$N1.Nodes.Add($newNode) | Out-Null
$newNode = New-Object System.Windows.Forms.TreeNode
$newNode.Name = 'Component 1'
$newNode.Text = 'Component 1'
$N1.Nodes.Add($newNode) | Out-Null
# There can be deeper levels of treenodes
$TV_AfterCheck = {
$treeView.Remove_AfterCheck($TV_AfterCheck)
checkChildNode($_.node)
checkParentNode($_.node)
$treeView.Add_AfterCheck($TV_AfterCheck)
}
$treeView.Add_AfterCheck($TV_AfterCheck)
$form.Controls.Add($treeView)
$form.ShowDialog()
Anybody know the reason why?