Skip to main content
Home Forums Silverlight Programming Programming with .NET - General Assigning a universal BeenClicked events to a set of buttons.
6 replies. Latest Post by jaa17 on September 17, 2009.
(0)
jaa17
Member
9 points
27 Posts
09-15-2009 10:02 AM |
I want to create a set of buttons, a bit like a number pad. I want to do this at runtime, creating the buttons at runtime for various reasons, because there are a lot of them and also I want to space and set them out depending on the size of the workspace.
However, these buttons are all related and I would like a universal Clicked function for the lot of them. I could create a new class
public class MyButton
{
int id;
Button button;
}
elsewhere
void UniversalBeenClick(MyButton button)
So the universal function would be able to know which button is pressed. But how do I allocated this function to all my manually-code-created buttons instead of having separate functions for each. Is there a way of doing this already, and it is somehow related to the arguments of the normal function, ie (object sender, RoutedEventArgs e).
Hope you can help.
Jon
mchlsync
Star
14606 points
2,730 Posts
09-15-2009 10:12 AM |
Why do you want to create new Button class?
I think you should be able to do as below ~
btn1.Click +=
btn2.Click +=
btn3.Click +=
09-16-2009 6:05 AM |
That is not the thing I am unsure about. OK, here is what I want to do. (I have not got my Silverlight documentation next to me at the moment, so the labels might be wrong. (Ie butts[ i ].Clicked += is the assigning the been clicked function to a button).
Button[] butts = new Button[10];
for(int i=0; i<10; i++)
butts[ i ] = new Button();
butts[ i ].Clicked += UniversalButtonClick; // Note this is the same button Been Clicked function for ALL buttons
else where
void UniversalButtonClick(object sender, RoutedEventArgs e)
// In here, how do I know which of my 10 buttons has been clicked????
09-16-2009 3:56 PM |
Bump
ksleung
Contributor
5376 points
1,028 Posts
09-16-2009 4:36 PM |
void UniversalButtonClick(object sender, RoutedEventArgs e) { Button button = sender as Button; // ... now do something about this button ...}
09-17-2009 1:14 AM |
jaa17:// In here, how do I know which of my 10 buttons has been clicked????
Hello,
You can use sender to identify the button.
09-17-2009 7:20 AM |
Thanks guys. :)