# \[WPF/C#\] Dynamic Buttons + Event Handling

**URL:** <https://forum.kirupa.com/t/wpf-c-dynamic-buttons-event-handling/216175>\
**Category:** open source\
**Created:** [February 14, 2007, 6:48am UTC](https://forum.kirupa.com/t/wpf-c-dynamic-buttons-event-handling/216175 "2007-02-14T06:48:07Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![kirupa](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/kirupa/32/11616_2.png) [@kirupa](https://forum.kirupa.com/u/kirupa)\
**Post date:** [February 14, 2007, 6:48am UTC](https://forum.kirupa.com/t/wpf-c-dynamic-buttons-event-handling/216175/1 "2007-02-14T06:48:07Z")

</div>

Hey everyone!  
In .NET, you don’t have something like eval or this[object] to dynamically map some string to an actual object. While it seems like that would make attaching events to dynamically created objects difficult, it is actually fairly straightforward.

First, here is the demo - [Click to download ([URL=“http://www.kirupafx.com/DynamicButtons/setup.exe”]If you are using a non-IE browser, click here](http://www.kirupafx.com/DynamicButtons/DynamicButtons.application)). You can hover over buttons, but the thing I want to focus on is when you click on a button. Before any of that, here is the obligatory screenshot:

![](http://forum.kirupa.com/uploads/kirupa/3339/7853d0bb12798436.png)

The code is:

```auto
public void populateButtons()
{
    int xPos;
    int yPos;
 
    Random ranNum = new Random();
 
    for (int i = 0; i < 50; i++)
    {
        Button foo = new Button();
        Style buttonStyle = Window.Resources["CurvedButton"] as Style;
 
        int sizeValue = ranNum.Next(50);
 
        foo.Width = sizeValue;
        foo.Height = sizeValue;
        foo.Name = "button" + i;
 
        xPos = ranNum.Next(300);
        yPos = ranNum.Next(200);
 
        foo.HorizontalAlignment = HorizontalAlignment.Left;
        foo.VerticalAlignment = VerticalAlignment.Top;
        foo.Margin = new Thickness(xPos, yPos, 0, 0);
 
        foo.Style = buttonStyle;
 
        **[COLOR=green]foo.Click += new RoutedEventHandler(buttonClick);[/COLOR]**
        LayoutRoot.Children.Add(foo);
   }
}
 
**[COLOR=green]private void buttonClick(object sender, EventArgs e)[/COLOR]**
**[COLOR=green]{[/COLOR]**
**[COLOR=green] Button clicked = (Button) sender;[/COLOR]**
**[COLOR=green] MessageBox.Show("Button's name is: " + clicked.Name);[/COLOR]**
**[COLOR=green]}[/COLOR]**
 

```

The code I have highlighted in green is really what is relevant for detecting a Mouse Click event and routing that event to the appropriate method, which in this case, is called **buttonClick**.

Notice that the first argument to buttonClick is of type object. That means, I need to typecast our object sender to the type Button before I can actually use it to access the properties I assigned in the populateButtons method. In the above example, if you click on a button, a MessageBox appears with the button’s name as determined earlier.

I have attached the Source file below 🙂

Cheers!  
Kirupa :goatee:

---

<div class="post-metadata">

**Author:** ![santosh82](https://avatars.discourse-cdn.com/v4/letter/s/c4cdca/32.png) [@santosh82](https://forum.kirupa.com/u/santosh82)\
**Post date:** [June 8, 2008, 7:46am UTC](https://forum.kirupa.com/t/wpf-c-dynamic-buttons-event-handling/216175/2 "2008-06-08T07:46:21Z")

</div>

if you could explain how you created the button style it will very helpful.

Thanks,  
Santosh

---

<div class="post-metadata">

**Author:** ![santosh82](https://avatars.discourse-cdn.com/v4/letter/s/c4cdca/32.png) [@santosh82](https://forum.kirupa.com/u/santosh82)\
**Post date:** [June 8, 2008, 9:12am UTC](https://forum.kirupa.com/t/wpf-c-dynamic-buttons-event-handling/216175/3 "2008-06-08T09:12:43Z")

</div>

Style buttonStyle = Window.Resources[“CurvedButton”] as Style;

the above line throws error in silverlight 2.0

---

<div class="post-metadata">

**Author:** ![kirupa](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/kirupa/32/11616_2.png) [@kirupa](https://forum.kirupa.com/u/kirupa)\
**Post date:** [June 8, 2008, 6:27pm UTC](https://forum.kirupa.com/t/wpf-c-dynamic-buttons-event-handling/216175/4 "2008-06-08T18:27:37Z")

</div>

This example only works in WPF. I will look into converting this into a SL2 example soon 🙂
