如何在C#中循环遍历多个复选框 [英] how loop through multiple checkbox in C#

查看:324
本文介绍了如何在C#中循环遍历多个复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在winfrom中有100个复选框。
它们的名称是连续的,如checkbox1,checkbox2等。
我的winform中有一个提交按钮。单击提交按钮后,它会检查是否选中了复选框,然后更新了某个值,否则更新了另一个值。
我必须选中100复选框。
所以我必须遍历100个复选框以检查该复选框是否已选中。

I have 100 checkbox in a winfrom. Their name is sequential like checkbox1,checkbox2 etc. I have a submit button in my winform. After clicking the submitting button, it checks, if a checkbox is checked then some value is updated otherwise another value is updated. I have to check 100 checkbox. So i have to loop through the 100 checkbox to check if the checkbox is checked or not.

我知道如何检查复选框

private void sumit_button_Click(object sender, EventArgs e)
{
     if (checkbox1.Checked)
     { 
        //  update 
     }
     else
     {  
        // update another  
     }

     if (checkbox2.Checked)
     {  
        //  update    
     }
     else
     {   
        // update another  
     }

     ......................and so on

} 

但是我怎么能用100个复选框呢???

But how can i do this for 100 checkbox???

推荐答案

foreach (var control in this.Controls) // I guess this is your form
            {
                if (control is CheckBox)
                {
                    if (((CheckBox)control).Checked)
                    {
                        //update
                    }
                    else
                    {
                        //update another
                    }
                }
            }

这篇关于如何在C#中循环遍历多个复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆