c# event handling between two forms

前端 未结 2 1143
执念已碎
执念已碎 2021-01-19 18:22

I have two forms and I am trying to capture an event generated from frmEventGenerate.cs in frmEventReceive.cs.

In this example I can receive the event from frmEven

2条回答
  •  醉酒成梦
    2021-01-19 19:18

    In your constructor, after instantiating frmEventGenerate:

    frmGen.Evt += ReceiveEvent;
    

    You don't need new LinkEventHandler(...) any more - as of C# 2, there's a method group conversion available which you can use to convert from a method group (the name of a method) to a delegate type.

    EDIT: I hadn't seen that your event was static. That suggests you should actually use:

    frmEventGenerate.Evt += ReceiveEvent;
    

    ... and you don't need the frmGen variable at all.

    However, I would strongly discourage you from this - why do you want the event to be static in the first place? (I'd also urge you to name your types more sensibly - something like "EventGenerator" would be better here, for example. Ignoring the convention that type names should be in Pascal case leads to confusing code.)

提交回复
热议问题