简体   繁体   English

C#切换大小写复选框

[英]C# Switch case on checkbox

I have following code which handles the CheckedChanged -event on 12 checkboxes. 我有以下代码来处理12个复选框上的CheckedChanged -event。

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: 您可以从Form.Controls集合中获取所有复选框的列表:

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

now iterate through the list and set the Checked property to true . 现在遍历列表,并将Checked属性设置为true This way all CheckedChanged events will be fired. 这样,将触发所有CheckedChanged事件。

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. 如果您使用WPF,则需要复选框的父控件。 It my example the Window has a Grid which is the parent of the checkboxes: 在我的示例中,“窗口”具有一个Grid ,该Grid是复选框的父级:

Grid myGrid = this.Content as Grid;

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

Beware in WPF the property is called IsChecked : 在WPF中要当心,该属性称为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. 一种可能的解决方案是创建一个字典,其中每个键元素是复选框的名称,而值是您要对该复选框执行的Action。

Now you don't need anymore che switch and you can simply call the method expected for that checkbox 现在,您不再需要che开关,只需调用该复选框所需的方法即可

// 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();
   }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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