Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.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# 使用Linq循环所有控件只获得第一个控件_C#_Linq - Fatal编程技术网

C# 使用Linq循环所有控件只获得第一个控件

C# 使用Linq循环所有控件只获得第一个控件,c#,linq,C#,Linq,我有一个有5个文本框和一个按钮的表单,我想检查所有这些文本框是否为空或空用户输入。 使用此简单方法: public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) {

我有一个有5个文本框和一个按钮的表单,我想检查所有这些文本框是否为空或空用户输入。 使用此简单方法:

public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (verifyUI() == true)
                MessageBox.Show("user input for all textboxes was correct!");
            else
                MessageBox.Show("user input for all textboxes was missing!");
        }

        private bool verifyUI()
        {
            bool userInputOk = false;
            foreach (Control cTxt in Controls.OfType<TextBox>())
            {
                if (string.IsNullOrWhiteSpace(cTxt.Text) || cTxt.Text == "")
                {
                    userInputOk = false;
                }
                else
                {
                    userInputOk = true;
                }
            }
            return userInputOk;
        }
    }
公共部分类表单1:表单
{
公共表格1()
{
初始化组件();
}
私有无效按钮1\u单击(对象发送者,事件参数e)
{
如果(verifyUI()==true)
Show(“所有文本框的用户输入正确!”);
其他的
Show(“缺少所有文本框的用户输入!”);
}
私有布尔验证UI()
{
bool userInputOk=false;
foreach(Controls.OfType()中的Control-cTxt)
{
if(string.IsNullOrWhiteSpace(cTxt.Text)| | cTxt.Text==“”)
{
userInputOk=false;
}
其他的
{
userInputOk=true;
}
}
返回userInputOk;
}
}
当我在文本框1中输入值时,该方法仅检查第一个文本框并返回true,而忽略所有其他文本框


我确信我正在使用的方法的逻辑有问题。

您的代码实际上只是在检查列表中的最后一个控件是否为空,因为这就是迭代结束的地方。

您似乎想知道是否有任何输入错误(或所有输入都正确):


在当前代码中,您不断地重新写入
userInputOk
,因此返回最后一个值

最简单的方法是使用
All
方法:

private bool verifyUI()
{
    return Controls.OfType<TextBox>().All(tb => !string.IsNullOrWhiteSpace(tb.Text));
}
private bool verifyUI()
{
返回控件.OfType().All(tb=>!string.IsNullOrWhiteSpace(tb.Text));
}

您还可以反转逻辑并使用
Any
方法。在这种情况下,我更喜欢
All
,因为它能更好地传达意图,让下一个人更清楚地理解逻辑。

您的代码只检查它找到的最后一个文本框。请尝试以下
All()
语句:

bool userInputOk = Controls.OfType<TextBox>()
                           .All(tb => !string.IsNullOrWhiteSpace(tb.Text));
bool userInputOk=Controls.OfType()
.All(tb=>!string.IsNullOrWhiteSpace(tb.Text));

请注意,
string.IsNullOrWhiteSpace()
对于
string.Empty
也是
true
。因此,您不需要对
string.Empty

进行额外检查。实际上,它可能在所有控件之间循环,但只返回最后一个控件的结果,因为如果控件具有有效文本,则将
userInputOk
的值设置回true。因此,最终结果是最后一个控件的结果。现在有可能
textBox1
是集合中的最后一个控件,这取决于它们是如何添加的。您可以删除
else
块,该块仅在控件具有无效值时才会标记
userInputOk
,也可以使用Linq:

 bool userInputOk = 
          !Controls.OfType<TextBox>()
                   .Any(cTxt => string.IsNullOrWhiteSpace(cTxt.Text))
bool userInputOk=
!控件。of type()的
.Any(cTxt=>string.IsNullOrWhiteSpace(cTxt.Text))

我想只是为了检查是否有文本框未填充。下面的代码就足够了

        private bool verifyUI()
        {
            bool alluserInputsOk = true;
            foreach (Control cTxt in Controls.OfType<TextBox>())
            {
                if (string.IsNullOrWhiteSpace(cTxt.Text))
                {
                    userInputOk = false;
                    break;
                }                   
            }
            return userInputOk;
        }
private bool verifyUI()
{
bool alluserInputsOk=真;
foreach(Controls.OfType()中的Control-cTxt)
{
if(string.IsNullOrWhiteSpace(cTxt.Text))
{
userInputOk=false;
打破
}                   
}
返回userInputOk;
}

或者您可以在列表中使用.any()方法。IsNullOrWhiteSpace(cTxt.Text)已经包含
cTxt.Text=“”
您看到
控件返回的内容了吗?您是否已检查您要查找的
文本框
是否在该集合中?也许如果它们嵌套在另一个
控件中
(如
面板
),则只有父控件出现在该列表中。为什么迭代会结束?感谢您的说明,如果我想检查所有控件,我能问您使用任何控件或全部控件有什么区别吗?@Ashraf Abusada:技术上没有区别,只是一种思考方式:如果没有发现错误的值(
Any
),则用户输入是正确的;或者用户输入是正确的,如果每个值都是正确的(
All
),我更喜欢后者使用
.All()
,因为它更直接地表达了意图。@Matthew Watson:这只是一种思维方式:我个人更喜欢
Any
——如果你找不到任何反例,输入是正确的。正确;不管怎样,都有一个恼人的
在逻辑中。:)考虑一下,我可能会调用方法
verificationFailed()
,然后去掉
(任意一个)然后调用站点可能看起来像
if(verificationFailed(){…handle failure…}
-事实上,
if(verifyUI()==true)
读得不好。无论如何,这是OP的问题,不是你的问题!
 bool userInputOk = 
          !Controls.OfType<TextBox>()
                   .Any(cTxt => string.IsNullOrWhiteSpace(cTxt.Text))
        private bool verifyUI()
        {
            bool alluserInputsOk = true;
            foreach (Control cTxt in Controls.OfType<TextBox>())
            {
                if (string.IsNullOrWhiteSpace(cTxt.Text))
                {
                    userInputOk = false;
                    break;
                }                   
            }
            return userInputOk;
        }