Page 1 of 1

[Unity] Casting sender to Button in its Click handler

Posted: 27 Jun 2014, 22:52
by Basp
If I define a button and add an event handler for its Click event, I get a method that has a sender parameter of type BaseComponent, and an e parameter of type RoutedEventArgs. Since I may have multiple buttons that all use this event handler, I want to figure out which button has fired the event. In WPF, I'd just cast the sender argument to a Button:
var button = sender as Button;
However, this doesn't work in NoesisGui. I can't seem to cast the BaseComponent to anything, and can't really do anything with the sender argument.

How do get a reference to the Button that fired the click event from the event handler?

Re: [Unity] Casting sender to Button in its Click handler

Posted: 27 Jun 2014, 22:58
by wckdspn
You can call on the BaseComponent As<Button>();

Re: [Unity] Casting sender to Button in its Click handler

Posted: 29 Jun 2014, 10:04
by Basp
Wow, I can't believe it was that simple. Thanks!

Re: [Unity] Casting sender to Button in its Click handler

Posted: 29 Jun 2014, 18:19
by Weltista
Plus one -- very helpful, thanks. (Had to use Noesis.Button.GetIsMouseOver() before that).

Re: [Unity] Casting sender to Button in its Click handler

Posted: 01 Jul 2014, 22:46
by jsantos
We are aware that this is not the standard way to cast in c#. It should work with the 'as' keyword. It is something we have in our To Do list.