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

 

_______________________________________________
users mailing list
[email protected]
http://lists.ironpython.com/listinfo.cgi/users-ironpython.com

Reply via email to