在组框Windows窗体中使用LINQ进行复选框验证 [英] Checkbox validation with LINQ inside a group box windows form

查看:92
本文介绍了在组框Windows窗体中使用LINQ进行复选框验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现可以验证linq是否选中了复选框.

i found it is possible to validate checkboxes is selected or not by linq.

我得到了类似的代码

btnAgree.Enabled = (from chkbox in Controls.OfType<CheckBox>() select chkbox).Count(b => b.Checked) >= 2;

但是我的情况就像我的获胜表格上有很多复选框,但是我想检查LINQ选择的组框内的所有复选框.有可能,但我仍然找不到办法.请帮助我提供代码.

but my scenario is like i have many check boxes on my win form but i want to check any check boxes inside my group box is selected by LINQ. is it possible but i found no way still. please help me with code.

我得到了一些有点相似的代码.代码如下

i got a little code which is bit similar. the code as follows

public static IEnumerable<T> AllControls<T>(this Control startingPoint) where T : Control
{
 bool hit = startingPoint is T;
 if (hit)
 {
    yield return startingPoint as T;
 }
 foreach (var child in startingPoint.Controls.Cast<Control>())
 {
    foreach (var item in AllControls<T>(child))
    {
       yield return item;
    }
 }

}

 var checkboxes = control.AllControls<CheckBox>();
 var checkedBoxes = control.AllControls<CheckBox>().Where(c => c.Checked);

但是如何自定义此代码,使其与我的所有"复选框所在的组框一起使用.请指导.另一个问题是我正在使用c#v2.0.谢谢

but how to customize this code that it will work with group box where my all check box is located. please guide. another issue is i am using c# v2.0. thanks

推荐答案

btnAgree.Enabled = 
    (from chkbox in groupBox1.Controls.OfType<CheckBox>()
     select chkbox).Count(b => b.Checked) >= 2;

您正在检查表单的控件.

You're checking the controls of the form.

这篇关于在组框Windows窗体中使用LINQ进行复选框验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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