如何计算组框中选中的复选框的数量 [英] how to count no of check box checked in a group box
本文介绍了如何计算组框中选中的复选框的数量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在一个分组框内,我使用了5个复选框.当用户尝试2单击大于2时,复选框消息应该像无法选择两个以上一样抛出.因此,用户必须取消选中已选择的任何一个,然后再选择另一个1
Inside a group box i used 5 checked box. when user try 2 click more than 2 check box message should throw like unable to choose more than two.,so user have to uncheck any of one which choosed already then choose another 1
推荐答案
简单-尽管可以控制它:
Easy - loop though it''s controls:
int count = 0;
foreach (Control c in myGroupBox.Controls)
{
CheckBox cb = c as CheckBox;
if (cb != null && cb.Checked)
{
count++;
}
}
我按照您所说的那样使用了上面的编码.但是所有5个复选框都被选中了."
添加这个...
"i used above coding as u said., but all the 5 checkboxes are getting check.,"
Add this...
if (count > 2)
{
cb = sender as CheckBox;
if (cb != null && cb.Checked)
{
cb.Checked = false;
}
errorLabel.Text = "You can only select two options";
}
这篇关于如何计算组框中选中的复选框的数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文