3

I installed XCode, MonoMac, Mono and MonoDevelop on a Mac OS X machine, and I cant for the love of me figure out how to successfully wire up a Click event to a simple button.

First, I control-dragged my button to the header file, and it created an outlet. Now, I can access my button (btnHello) from C# code, thats cool - but how, and where, do I bind events to it?

I tried doing

this.btnHello.PerformClick += delegate {
    this.Title = "Hello World";
};

but MonoDevelop complains about I cant do that, due to some "method group" stuff.

I have been researching this for days, and I cant find any tutorials on how to do this, and I dont want to use sample code as it does not give me the process of how to do this properly in the future. I tried looking at the MonoTouch getting started guide, since it is close to MonoMac, but that did not help either.

Any help would be great, especially regarding outlets and actions, and how I can wire them up to my elements! Thank you!

Jeff
  • 12,085
  • 12
  • 82
  • 152

3 Answers3

3

Try:

    this.btnHello.Activated += delegate {
    this.Title = "Hello World";
};

This code should go somewhere in your Controller, most likely in AwakeFromNib.

TrustMe
  • 261
  • 1
  • 3
  • So its the Activated event! Where should one store this beauty? The MainWindowController, or the MainWindow? And if you dont mind, what method? I think that info will add great value to your answer Sir. :) – Jeff Sep 17 '12 at 06:59
2

Most of the issue probably stems from the fact that most tutorials you find on the web still reference older versions of MonoDevelop, XCode and Interface Builder.

I've posted this link previously and it shows how to get a basic project going, including event handlers with the new XCode 4.2+ setup.

http://tufnelltech.blogspot.ca/2012/01/hello-os-x-from-c-five-steps.html

kdmurray
  • 2,988
  • 3
  • 32
  • 47
0

NSButton.PerformClick() is a method, not an event, so you can't connect a delegate to it.

I'm not familiar enough with the Mac APIs to suggest how to do what you want, unfortunately.

jstedfast
  • 35,744
  • 5
  • 97
  • 110
  • Simply looking for a way to wire up a click event to a button - in Visual studio, I just doubleclick my button and the event code is generated, ready for use. But, like all things developer-related from Apple, they won't let it be that easy.. :( – Jeff Sep 12 '12 at 06:06