我怎样才能prevent从射击在C#中导致自己的事件的事件? [英] How can I prevent an event from causing its own event from firing in C#?

查看:102
本文介绍了我怎样才能prevent从射击在C#中导致自己的事件的事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个复选框TreeView和我有以下处理程序AfterCheck事件:

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();
        }
    }
}

如果你仔细观察,你会看到我,如果_isCheckingInProgress检查。如果不是,那我继续,并展开所有的节点和调用SetChildrenCheckState()方法。我所遇到的问题是,SetChildrenCheckState()也将随之导致每个子节点的所有火AfterCheck事件其自己的节点。

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.

我的问题是,是否有一个更清洁的方式来使第一AfterCheck触发事件,但没有后续的?似乎有种hackish的,我必须有一个实例布尔变量来检查和设置。

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.

推荐答案

一个建议,你会看到周围偶尔SO是不是把大量的code到事件处理程序本身。有许多的原因。首先,在你的情况下,它会更容易理解像一个电话:

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:

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

和放置在 GetAvailableFiles你的code中的其余部分()。这就形成之间的事件code和行动code分离,大多数人都会同意是一个值得区别之作。

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.

其次,其可以或可以不适用在你的情况是,多个事件可能导致同样的动作。如 mnuFileQuit_Click btnClose_Click 作为一个明显的例子。如果同时拨打 CloseApplication()它消除了大量的冗余code。

Second, which may or may not be applicable in your case is that multiple events can cause the same action. Such as mnuFileQuit_Click and btnClose_Click as an obvious example. If both make calls to CloseApplication() it removes a lot of redundant code.

这篇关于我怎样才能prevent从射击在C#中导致自己的事件的事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆