How can I prevent the event that triggered the start of my own event in C #?

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.

+5
4

, SO, - . . -, :

private void trvAvailableFiles_AfterCheck(object sender, TreeViewEventArgs e)
{
    if (!_isCheckingInProgress) 
    {
        _isCheckingInProgress = true;
        try { GetAvailableFiles(); } catch {}
        _isCheckingInProgress = false;
    }
}

GetAvailableFiles(). , , .

-, , , . , mnuFileQuit_Click btnClose_Click, . CloseApplication(), .

+4

if(e.Action != TreeViewAction.Unknown) if (!_isCheckingInProgress). . TreeViewAction.

, e.Action TreeViewAction.ByKeyboard TreeViewAction.ByMouse.

MSDN TreeView.AfterCheck.

1: , , , . - , , .

2: . Spencer Edit 1

+6

, .

private void trvAvailableFiles_AfterCheck(object sender, TreeViewEventArgs e)
{
    EnableEvents(false);
    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();

        SetChildrenCheckState(e.Node, isChecked);

    }
    finally
    {
        trvAvailableFiles.EndUpdate();
    }
    EnableEvents(true);
}

private void EnableEvents(bool bEnable)
{
    if(bEnable)
        cbWhatever.OnChecked += EventHandler;
    else
        cbWhatever.OnChecked -= EventHandler;
}
+2

, , . , , - "" . - , .

- , . , ; "" .

+1

All Articles