DCSIMG
Bubbling Windows Forms Events With Anonymous Methods - עומר.נט

Bubbling Windows Forms Events With Anonymous Methods

Posted Mar 08 2007, 05:24 PM by עומר ון קלוטן  

The following code illustrates a great use for anonymous methods.

public class MyContainerControl : Control
{
    private class MySpecializedControl : Control
    {
        protected virtual void OnMyEvent(EventArgs e)
        {
            if (this.MyEvent != null)
                this.MyEvent(this, e);
        }

        public event EventHandler MyEvent;
    }

    private MySpecializedControl special;

    public MyContainerControl()
    {
        this.special = new MySpecializedControl();
        // ...
        this.Controls.Add(this.special);

        this.special.MyEvent += delegate(object sender, EventArgs e) { this.OnMyEvent(e); };
    }

    protected virtual void OnMyEvent(EventArgs e)
    {
        if (this.MyEvent != null)
            this.MyEvent(this, e);
    }

    public event EventHandler MyEvent;
}
[Cross-Posted from Omer van Kloeten's .NET Zen, my English language weblog]


שלח תגובה

(שדה חובה) 

(שדה חובה) 

(אופציונלי)

(שדה חובה) 


Enter the numbers above: