Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/291.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在组框windows窗体中使用LINQ进行复选框验证_C#_Linq - Fatal编程技术网

C# 在组框windows窗体中使用LINQ进行复选框验证

C# 在组框windows窗体中使用LINQ进行复选框验证,c#,linq,C#,Linq,我发现可以验证linq是否选中复选框 我的密码是这样的 btnAgree.Enabled = (from chkbox in Controls.OfType<CheckBox>() select chkbox).Count(b => b.Checked) >= 2; 但我的情况是,我的赢单上有很多复选框,但我想选中组框中的任何复选框,因为组框是由LINQ选中的。有没有可能,但我还是找不到办法。请帮我写代码 我得到了一个有点类似的代码。代码如下所示 public stat

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

我的密码是这样的

btnAgree.Enabled = (from chkbox in Controls.OfType<CheckBox>() select chkbox).Count(b => b.Checked) >= 2;
但我的情况是,我的赢单上有很多复选框,但我想选中组框中的任何复选框,因为组框是由LINQ选中的。有没有可能,但我还是找不到办法。请帮我写代码

我得到了一个有点类似的代码。代码如下所示

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;
    }
 }
}

但如何自定义此代码,使其与“我的全部”复选框所在的组框一起工作。请导游。另一个问题是我使用的是c v2.0。谢谢

btnAgree.Enabled = 
    (from chkbox in groupBox1.Controls.OfType<CheckBox>()
     select chkbox).Count(b => b.Checked) >= 2;
您正在检查表单的控件


您正在检查表单的控件。

您不需要使用表单的控件集合,可以使用您的:


您不需要使用表单的控件集合,您可以使用您的控件集合:


若你们需要递归,想象你们的GroupBox中有一个面板,并且复选框已经添加到面板中,我认为你们的扩展方法仍然是有用的

有点变了

public static IEnumerable<T> AllControls<T>(this Control startingPoint) where T : Control
        {
            if (startingPoint is T)
                yield return startingPoint as T;

            foreach (var item in startingPoint.Controls.Cast<Control>().SelectMany(AllControls<T>))
                yield return item;
        }
用法:

var list = groupBox1.AllControls<CheckBox>();
var listChecked = list.Where(m => m.Checked);

若你们需要递归,想象你们的GroupBox中有一个面板,并且复选框已经添加到面板中,我认为你们的扩展方法仍然是有用的

有点变了

public static IEnumerable<T> AllControls<T>(this Control startingPoint) where T : Control
        {
            if (startingPoint is T)
                yield return startingPoint as T;

            foreach (var item in startingPoint.Controls.Cast<Control>().SelectMany(AllControls<T>))
                yield return item;
        }
用法:

var list = groupBox1.AllControls<CheckBox>();
var listChecked = list.Where(m => m.Checked);
只使用Countb=>b.Checked>0有什么问题?只使用Countb=>b.Checked>0有什么问题?