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 closely you’ll see that I’m checking if “_isCheckingInProgress”. If it is not, then I proceed and expand all the nodes and call the SetChildrenCheckState() method. The problem I have encountered is that SetChildrenCheckState() will subsequently cause each child node to all fire the AfterCheck event for its own node.
My question is, is there a more clean way to allow the first AfterCheck event to fire but not the subsequent ones? It seems kind of hackish that I have to have an instance bool variable to check and set.
One recommendation you’ll see occasionally around SO is to not put a lot of code into event handlers themselves. There are a number of reasons for this. First, in your case it would be easier to understand a call like:
And to place the rest of your code in
GetAvailableFiles(). This creates a separation between event code and action code which most people would agree is a worthwhile distinction to make.Second, which may or may not be applicable in your case is that multiple events can cause the same action. Such as
mnuFileQuit_ClickandbtnClose_Clickas an obvious example. If both make calls toCloseApplication()it removes a lot of redundant code.