C#, How to create an event and listen for it in another class?

后端 未结 3 1583
小鲜肉
小鲜肉 2021-01-15 08:09

I can\'t figure out how to do this, heres sample code. Of what I wish to do.

public Class MainForm : Form
{
    MyUserControl MyControl = new MyUserControl;
         


        
相关标签:
3条回答
  • 2021-01-15 08:24
    //listen for MyEvent from MainForm, and perform MyMethod
    

    That's the wrong way around. Publishing an event in control is useful, the control cannot possibly guess how it is going to get used. It however most certainly should not know anything about an event that may or may not be available in the form that it gets dropped on. That has the nasty habit of blowing up when the form just doesn't (yet) have the event. The bad kind too, a crash at design time that puts up the White Screen of Darn and prevents you from fixing the problem.

    A form doesn't have to guess, it knows exactly what controls it has. So where ever in the form you might want to raise the event, just call the control's MyMethod method directly. And if that's wrong for some reason, like removing the control but not the call, then you just get a compile error that's easy to fix.

    0 讨论(0)
  • 2021-01-15 08:41

    This is how to delegate to an event of a private member, so the outside can listen to it.

    public event EventHandlerType EventHandlerName
    {
        add
        {
            this._privateControl.EventHandlerName += value;
        }
        remove
        {
            this._privateControl.EventHandlerName -= value;            
        }
    }
    

    Another option would be to have an event in your form class:

    public event EventHandler MyEvent;
    

    And listen to the private member's event:

    this._customControl.SomeEvent += this.SomeEventHandler;
    

    With this:

    private void SomeEventHandler(object sender, EventArgs e)
    {
        if (this.MyEvent != null)
        {
            this.MyEvent(this, e);
        }
    }
    

    The usage from the outside in both cases will be the same:

    var form = new Form1();
    
    form1.MyEvent += (o, e) => { Console.WriteLine("Event called!"); };
    

    The bottom line is the you must implement functionality inside your form to allow the outside subscribe/listen to inner events.

    0 讨论(0)
  • 2021-01-15 08:47

    Step 1) Expose an event on MainForm... say..

    public event Action simpleEvent
    

    Step 2) Give MyUserControl a constructor that takes an instance of MainForm and bind an action to that event

    public MyUserControl(MainForm form) {
        form += () => Console.WriteLine("We're doing something!")
    }
    

    Step 3) raise the event in MainForm.Button_Click

    if(simpleEvent != null) simpleEvent();
    

    Note: You could register your own delegates and work with something other than lambda expressions. See http://msdn.microsoft.com/en-us/library/17sde2xt.aspx for a more thorough explanation

    Your end result would look like...

    public Class MainForm : Form
    {
        public event Action MyEvent;
    
        MyUserControl MyControl = new MyUserControl(this);
        private void Button_Click(object sender, EventArgs e)
        {
            if(simpleEvent != null) simpleEvent();
        }    
    }
    
    public Class MyUserControl : UserControl
    {
        //listen for MyEvent from MainForm, and perform MyMethod
        public MyUserControl(MainForm form) {
            simpleEvent += () => MyMethod();
        }
    
        public void MyMethod()
        {
             //Do Stuff here
        }
    }
    
    0 讨论(0)
提交回复
热议问题