[IronPython] .NET Events in IronPython

Michael Shilman shilman at microsoft.com
Fri Nov 11 21:58:59 CET 2005


Is there any way to declare .NET events in IronPython?

 

Here is a simple example in C# that "abstracts" a button pressed event
into a "hello" event:

 

    public delegate void HelloInvoked();

    public class HelloControl : Panel

    {

        public event HelloInvoked Hello;

        public HelloControl()

        {

            Button b = new Button();

            b.Text = "Hello";

            b.Click += new EventHandler(b_Click);

            b.Dock = DockStyle.Fill;

            this.Controls.Add(b);

        }

 

        void b_Click(object sender, EventArgs e)

        {

            if (Hello != null) Hello();

        }

    }

 

I can do something equivalent with Python functions:

 

class HelloControl(Panel):

     Hello = []

     def __init__(self):

          b = Button(Text="Hello",Dock=DockStyle.Fill)

          b.Click += self.on_Click

          self.Controls.Add(b)

     def on_Click(self,sender,e):

          for h in self.Hello: h()

def Foo(): print "hello"

hc.Hello += [Foo]

 

But for consistency with my other C# code I'd like to use events if
possible. 

 

Any thoughts?

 

Many thanks,

 

Michael

 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/ironpython-users/attachments/20051111/33121ecd/attachment.html>


More information about the Ironpython-users mailing list