1

I created a checkbox list with 8 chechboxes added dynamically. The idea of my program is : when a box is checked, a chart appears on my plotter, when i uncheck it, it disappears.

My problem is that I dont know how to manage the events to do that because I added the checkboxes dynamically and I need 8 different events for 8 different charts.

Thanks.

Jean Col
  • 522
  • 5
  • 16

3 Answers3

2

You can use one event for all of them. And inside of the event you will get the name of the control, which fired the event. Something like this:

 private void CheckBox_Click(object sender, RoutedEventArgs e)
 {
     CheckBox senderChk = sender as CheckBox;
     switch (senderChk.Name)
     {
         case "checkBox1":  //do something 
         case "checkBox2":  //do something 
     }
 }
Tim S.
  • 55,448
  • 7
  • 96
  • 122
Farhad Jabiyev
  • 26,014
  • 8
  • 72
  • 98
1

An answer here suggests using .Name property but for dynamically-created checkboxes that may now work well.

CheckBox chx;
chx.Tag = "Chart 1"; // put these tags in an enum or at least constants
chx.Click += chx_Click; 

void chx_Click(object sender, RoutedEventArgs e)
{
    CheckBox chx = sender as CheckBox;
    if (chx != null && chx.Tag != null)
    {
        switch (chx.Tag)
        {
            case "Chart 1": 
                        myChart1.Visibility = chx.IsChecked? Visibility.Visible: Visibility.Collapsed;  
                break;
            case "Chart 2": //...
                break;
            default:
                break;
        }
    }
}
Sten Petrov
  • 10,943
  • 1
  • 41
  • 61
  • 1
    Sorry but this is also a terribly bad solution, which uses WPF in an even worse winforms way. I mean, the Tag property? please... – Federico Berasategui Feb 15 '13 at 17:17
  • Is the use of Tag property all you have against? I mean OP already abused WPF by dynamically creating checkboxes instead of defining them in XAML, possibly templated, binding the IsChecked to Visibility with a property converter. He asked a question, I answered, doesn't mean I agree with his design. – Sten Petrov Feb 15 '13 at 17:37
  • right. The supposed "dynamic" UI is the problem to begin with. – Federico Berasategui Feb 15 '13 at 17:39
0

The "sender" parameter of the event handler indicates which control raised an event.

Somewhere, you create a control. Make sure you keep a reference to it somewhere, either as a member variable, in a dictionary or whatever.

Then, in your event handler, do the following:

If(sender==myControl)
{
   ...do something...
}
Elseif (sender==myOtherControl)
{
    ...do something else...
}
Immortal Blue
  • 1,691
  • 13
  • 27
  • Sorry but this is a terribly bad solution, which uses WPF in a winforms way. – Federico Berasategui Feb 15 '13 at 16:55
  • @HighCore, probably a fair comment, but please explain *why* this is a terrible way? – Immortal Blue Feb 15 '13 at 17:07
  • 1
    @ImmortalBlue because [UI is Not Data](http://stackoverflow.com/questions/14381402/wpf-programming-methodology/14382137#14382137). He should be using an `ItemsControl` with a proper `DataTemplate` to begin with, then he could program his application logic against `Data Items` instead of `UI`. – Federico Berasategui Feb 15 '13 at 17:10