简体   繁体   中英

C# Switch case on checkbox

I have following code which handles the CheckedChanged -event on 12 checkboxes.

private void cbJournal_CheckedChanged(object sender, RoutedEventArgs e)
{
    CheckBox chk = (CheckBox)sender;

    if (chk.IsChecked == true)
    {

        switch (chk.Name)
        {
            case "cbErbbstg":
            {
                month = Months[idxMonth];
                LinkToRSS.Add(link + month);
                RSSname.Add(name);
                Picture.Add(picture);
                ID.Add(100);

            }
            break;

            case "cbGstb":
            {
                month = Months[idxMonth];
                LinkToRSS.Add(link + month);
                RSSname.Add(name);
                Picture.Add(picture);
                ID.Add(200);
            }
            break;
      }
}

Now I want to implement a checkbox which, if checked, checks every other checkbox too, and adds all elements to the lists.

That is, I want to iterate through all cases somehow, so every list can be populated with values.

EDIT: Just fyi: the values added (name, link, etc) are different ones for every checkbox.

Any suggestions?

you can get the list of all checkboxes from the Form.Controls collection:

List<CheckBox> chbList = this.Controls.OfType<CheckBox>().ToList();

now iterate through the list and set the Checked property to true . This way all CheckedChanged events will be fired.

foreach (var chb in chbList)
{
    chb.Checked = true;
}

if you have hooked them to the same event handler you can call the method also directly:

foreach (var chb in chbList)
{
    cbJournal_CheckedChanged(chb, new RoutedEventArgs());
}

EDIT:

If you aer using WPF you need the parent control of the Checkboxes. It my example the Window has a Grid which is the parent of the checkboxes:

Grid myGrid = this.Content as Grid;

List<CheckBox> chbList = myGrid.Children.OfType<CheckBox>().ToList();

Beware in WPF the property is called IsChecked :

foreach (var chb in chbList)
{
    chb.IsChecked = true;
}

A possible solution is to create a dictionary where each key element is the name of the checkbox while the value is the Action you want to perform for that checkbox.

Now you don't need anymore che switch and you can simply call the method expected for that checkbox

// Declare this at the class global level
Dictionary<string, Action> checkExecuter = new Dictionary<string, Action>();

// Initialize it somewhere at the start of your class lifetime
checkExecuter.Add("cbErbbstg", OnCbErbbstgChecked);
// add other methods here
checkExecuter.Add("cbAllBox", OnAllBox);

// Action to execute when the cbErbbstg is checked
void OnCbErbbstgChecked()
{
    month = Months[idxMonth];
    LinkToRSS.Add(link + month);
    RSSname.Add(name);
    Picture.Add(picture);
    ID.Add(100);
}

// Action to execute when the cbAllBox is checked
void onAllBox()
{
   foreach(KeyValuePair<string, Action> kvp in checkExecuter)
   {
        if(kvp.Key != "cbAllBox")
           kvp.Value.Invoke();
   }
}

private void cbJournal_CheckedChanged(object sender, RoutedEventArgs e)
{
   CheckBox chk = (CheckBox)sender;
   if (chk.IsChecked && checkExecuter.ContainsKey(chk.Name))
   { 
       checkExecuter[chk.Name].Invoke();
   }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM