I have a treeview with checkboxes, and I have the following handler for the AfterCheck event:
private void trvAvailableFiles_AfterCheck(object sender, TreeViewEventArgs e)
{
if (!_isCheckingInProgress)
{
trvAvailableFiles.BeginUpdate();
var nodePath = e.Node.Tag.ToString();
bool isChecked = e.Node.Checked;
e.Node.Nodes.Clear();
try
{
_fileTreeLogic.GetChildNodes(e.Node, true);
e.Node.ExpandAll();
_isCheckingInProgress = true;
SetChildrenCheckState(e.Node, isChecked);
_isCheckingInProgress = false;
}
finally
{
trvAvailableFiles.EndUpdate();
}
}
}
If you look carefully, you will see that I check that "_isCheckingInProgress". If this is not the case, I continue and expand all the nodes and call the SetChildrenCheckState () method. The problem I ran into is that SetChildrenCheckState () will subsequently cause each child element of the node to fire an AfterCheck event for its own node.
My question is, is there a cleaner way to allow the first AfterCheck event to fire, but not the subsequent ones? It seems to be hacks that I must have an instance bool variable for verification and installation.