Skip to main content

Microsoft Silverlight

Answered Question Assigning a universal BeenClicked events to a set of buttons.RSS Feed

(0)

jaa17
jaa17

Member

Member

9 points

27 Posts

Assigning a universal BeenClicked events to a set of buttons.

 

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
mchlsync

Star

Star

14606 points

2,730 Posts

Silverlight MVP

Re: Assigning a universal BeenClicked events to a set of buttons.

Why do you want to create new Button class?

I think you should be able to do as below ~

Button btn1 = new Button();

btn1.Click += new RoutedEventHandler(UniversalBeenClick);

Button btn2 = new Button();

btn2.Click += new RoutedEventHandler(UniversalBeenClick);

Button btn3 = new Button();

btn3.Click += new RoutedEventHandler(UniversalBeenClick);

(If this has answered your question, please click on "Mark as Answer" on this post. Thank you!)

Regards,
Michael Sync
Silverlight MVP

Blog : http://michaelsync.net


jaa17
jaa17

Member

Member

9 points

27 Posts

Re: Assigning a universal BeenClicked events to a set of buttons.

 

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????

}

 

 

jaa17
jaa17

Member

Member

9 points

27 Posts

Re: Assigning a universal BeenClicked events to a set of buttons.

Bump

 

ksleung
ksleung

Contributor

Contributor

5376 points

1,028 Posts

Answered Question

Re: Re: Assigning a universal BeenClicked events to a set of buttons.

void UniversalButtonClick(object sender, RoutedEventArgs e) {
    Button button = sender as Button;
    // ... now do something about this button ...
}

mchlsync
mchlsync

Star

Star

14606 points

2,730 Posts

Silverlight MVP
Answered Question

Re: Assigning a universal BeenClicked events to a set of buttons.

jaa17:
// In here, how do I know which of my 10 buttons has been clicked????

Hello,

You can use sender to identify the button.

(If this has answered your question, please click on "Mark as Answer" on this post. Thank you!)

Regards,
Michael Sync
Silverlight MVP

Blog : http://michaelsync.net


jaa17
jaa17

Member

Member

9 points

27 Posts

Re: Re: Assigning a universal BeenClicked events to a set of buttons.

Thanks guys. :)

  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities