I ran into an issue with the classic approach to synchronized checking
of child nodes within a handler for the aftercheck event.
After completing all the checkbox logic, I wanted to perform an
expensive formatting function, but due to the nature of the event
handler logic, I was inadvertently running the same formatting during
each checkbox event.
Here's the solution I came up with, it appears to work, but I'm
interested to see if there are any obvious flaws, or a smarter method
of doing this.
private void treeView_AfterCheck(object sender,
System.Windows.Forms.TreeViewEventArgs e)
{
foreach (TreeNode subNode in targetNode.Nodes)
{
// nestCount is an int=0, defined in the class
nestCount++;
subNode.Checked = targetNode.Checked;
nestCount--;
}
if (nestCount==0)
{
//Do expensive one-time operation here
}
}