Prevent next event handler being called

后端 未结 6 1202
孤街浪徒
孤街浪徒 2021-01-21 01:35

I have two event handlers wired up to a button click in a Windows form like so:

this.BtnCreate.Click += new System.EventHandler(new RdlcCreator().FirstHandler);
         


        
6条回答
  •  鱼传尺愫
    2021-01-21 02:05

    Why don't you just replace them with one eventhandler? Something like this:

    var rdlc = new RdlcCreator();
    this.BtnCreate.Click += (sender, e) => {
        rdlc.FirstHandler(sender, e);
        if (!rdlc.HasHandledStuff) { // <-- You would need some kind of flag 
            this.BtnCreate_Click(sender, e);
        }
    };
    

    That way you can also guarantee the order of the handlers. Alternatively, use the above implementation, but change the signature of FirstHandler to return a bool indicating the condition (as in this case it doesn't really need to have the event's signature anymore):

        if (!rdlc.FirstHandler(sender, e)) { 
            this.BtnCreate_Click(sender, e);
        }
    

    EDIT: OR, you just pass the second handler to FirstHandler.
    Change the signature of FirstHandler to this:

    void FirstHandler(object sender, EventArgs e, EventHandler nextHandler) {
        if (ConditionSatisfied) {
            // do stuff
        }
        else if (nextHandler != null) {
            nextHandler(sender, e);
        }
    }
    

    and then:

    this.BtnCreate.Click += 
        (s, e) => new RdlcCreator().Firsthandler(s, e, this.BtnCreate_Click);
    

提交回复
热议问题