如何在Windows应用程序中的C#.net中获得选择的复选框计数 [英] how to get selcted check boxes count in C#.net in windows apllication

查看:49
本文介绍了如何在Windows应用程序中的C#.net中获得选择的复选框计数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,
我想被选中Windows中的复选框计数是否可以引导或发送任何代码段

Hello,
I want to get selected Check boxes count in windows Can u guide or send any snippets

推荐答案

如果将复选框直接添加到表单中,则可以使用此功能. br/>
if the checkboxes are added directly to the form then you can use this.
int counter = 0;
           foreach (Control c in this.Controls)
               if (c is CheckBox)
                   if (((CheckBox)c).Checked)
                       counter++;

           MessageBox.Show(counter.ToString());


如果要求计数为CheckedCheckBoxes,则可以使用以下LINQ查询

If the requirement is to count the CheckBoxes which are Checked, then the following LINQ query can be used

for(int i=0; i< 10; i++){
    CheckBox chkBox = new CheckBox();
    if (i % 2==0)
        chkBox.Checked = true;
    Controls.Add(chkBox);
}
Controls.Add(new TextBox());
Controls.Add(new Button());

int selectedCheckBoxesCount = Controls.OfType<CheckBox>()
                            .Count (cb => cb.Checked);

//selectedCheckBoxesCount
//5


通过所有控件进行循环在窗体上,找到每个复选框,如果已选中,则将计数器加1.

请参阅 [ ^ ]链接.
Loop through all controls on form, find each check box, if its checked then increment counter by 1.

Refer this[^] link.


这篇关于如何在Windows应用程序中的C#.net中获得选择的复选框计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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