C# 选中所有文本框';不包含空格/空值

C# 选中所有文本框';不包含空格/空值,c#,string,forms,function,C#,String,Forms,Function,我有一个表单,它有多个文本框,我想循环遍历文本框,看看其中是否有包含null或空格的。如果他们这样做了,那么我希望返回一个布尔值 我得到的错误是: {“无法将'System.Windows.Forms.Button'类型的对象强制转换为'System.Windows.Forms.TextBox'。”}{“无法强制转换” 类型为“System.Windows.Forms.Button”的对象,以键入 'System.Windows.Forms.TextBox'。“} 我的代码: p

我有一个表单,它有多个文本框,我想循环遍历文本框,看看其中是否有包含null或空格的。如果他们这样做了,那么我希望返回一个布尔值

我得到的错误是: {“无法将'System.Windows.Forms.Button'类型的对象强制转换为'System.Windows.Forms.TextBox'。”}{“无法强制转换” 类型为“System.Windows.Forms.Button”的对象,以键入 'System.Windows.Forms.TextBox'。“}

我的代码:

        private bool EmptyTextBox()
    {
        //returns false if all the text boxs contain strings otherwise it will set the messagebox then return true
        /*if (!Controls.Cast<TextBox>().Any(textBox => String.IsNullOrWhiteSpace(textBox.Text))) return false;
        MessageBox.Show("Please do not leave a textbox blank.");*/
        foreach (TextBox textBox in this.Controls)
        {
            if (string.IsNullOrWhiteSpace(textBox.Text))
            {
                MessageBox.Show("Please do not leave a textbox blank.");
                return false;

            }
        }
        return true;
    }
private bool EmptyTextBox()
{
//如果所有文本框都包含字符串,则返回false,否则将设置messagebox,然后返回true
/*if(!Controls.Cast().Any(textBox=>String.IsNullOrWhiteSpace(textBox.Text)))返回false;
Show(“请不要将文本框留空。”)*/
foreach(此.Controls中的文本框)
{
if(string.IsNullOrWhiteSpace(textBox.Text))
{
Show(“请不要将文本框留空。”);
返回false;
}
}
返回true;
}

我想知道我做错了什么,以及如何解决这个问题,谢谢。

这个。控件将返回所有类型的控件<代码>按钮
文本框
组合框
等等。在
foreach
语句中,您正在将所有控件强制转换为
TextBox
。这就是这个例外的原因。您只需要获取
TextBox
控件。 您可以使用
System.Linq
命名空间中的
Enumerable.OfType

将代码更改为:

 foreach (TextBox textBox in this.Controls.OfType<TextBox>())
        {
            if (string.IsNullOrWhiteSpace(textBox.Text))
            {
                MessageBox.Show("Please do not leave a textbox blank.");
                return false;
            }
        }
foreach(此.Controls.OfType()中的文本框)
{
if(string.IsNullOrWhiteSpace(textBox.Text))
{
Show(“请不要将文本框留空。”);
返回false;
}
}
或者,如果您喜欢单行代码:

private bool EmptyTextBox()
{
     bool result = this.Controls.OfType<TextBox>().Any(x => string.IsNullOrWhiteSpace(x.Text));

     if(result==true) MessageBox.Show("Please do not leave a textbox blank.");

     return !result;
}
private bool EmptyTextBox()
{
bool result=this.Controls.OfType().Any(x=>string.IsNullOrWhiteSpace(x.Text));
if(result==true)MessageBox.Show(“请不要将文本框留空”);
返回!结果;
}

此控件将返回所有类型的控件<代码>按钮
文本框
组合框
等等。在
foreach
语句中,您正在将所有控件强制转换为
TextBox
。这就是这个例外的原因。您只需要获取
TextBox
控件。 您可以使用
System.Linq
命名空间中的
Enumerable.OfType

将代码更改为:

 foreach (TextBox textBox in this.Controls.OfType<TextBox>())
        {
            if (string.IsNullOrWhiteSpace(textBox.Text))
            {
                MessageBox.Show("Please do not leave a textbox blank.");
                return false;
            }
        }
foreach(此.Controls.OfType()中的文本框)
{
if(string.IsNullOrWhiteSpace(textBox.Text))
{
Show(“请不要将文本框留空。”);
返回false;
}
}
或者,如果您喜欢单行代码:

private bool EmptyTextBox()
{
     bool result = this.Controls.OfType<TextBox>().Any(x => string.IsNullOrWhiteSpace(x.Text));

     if(result==true) MessageBox.Show("Please do not leave a textbox blank.");

     return !result;
}
private bool EmptyTextBox()
{
bool result=this.Controls.OfType().Any(x=>string.IsNullOrWhiteSpace(x.Text));
if(result==true)MessageBox.Show(“请不要将文本框留空”);
返回!结果;
}

此控件将返回所有类型的控件<代码>按钮
文本框
组合框
等等。在
foreach
语句中,您正在将所有控件强制转换为
TextBox
。这就是这个例外的原因。您只需要获取
TextBox
控件。 您可以使用
System.Linq
命名空间中的
Enumerable.OfType

将代码更改为:

 foreach (TextBox textBox in this.Controls.OfType<TextBox>())
        {
            if (string.IsNullOrWhiteSpace(textBox.Text))
            {
                MessageBox.Show("Please do not leave a textbox blank.");
                return false;
            }
        }
foreach(此.Controls.OfType()中的文本框)
{
if(string.IsNullOrWhiteSpace(textBox.Text))
{
Show(“请不要将文本框留空。”);
返回false;
}
}
或者,如果您喜欢单行代码:

private bool EmptyTextBox()
{
     bool result = this.Controls.OfType<TextBox>().Any(x => string.IsNullOrWhiteSpace(x.Text));

     if(result==true) MessageBox.Show("Please do not leave a textbox blank.");

     return !result;
}
private bool EmptyTextBox()
{
bool result=this.Controls.OfType().Any(x=>string.IsNullOrWhiteSpace(x.Text));
if(result==true)MessageBox.Show(“请不要将文本框留空”);
返回!结果;
}

此控件将返回所有类型的控件<代码>按钮
文本框
组合框
等等。在
foreach
语句中,您正在将所有控件强制转换为
TextBox
。这就是这个例外的原因。您只需要获取
TextBox
控件。 您可以使用
System.Linq
命名空间中的
Enumerable.OfType

将代码更改为:

 foreach (TextBox textBox in this.Controls.OfType<TextBox>())
        {
            if (string.IsNullOrWhiteSpace(textBox.Text))
            {
                MessageBox.Show("Please do not leave a textbox blank.");
                return false;
            }
        }
foreach(此.Controls.OfType()中的文本框)
{
if(string.IsNullOrWhiteSpace(textBox.Text))
{
Show(“请不要将文本框留空。”);
返回false;
}
}
或者,如果您喜欢单行代码:

private bool EmptyTextBox()
{
     bool result = this.Controls.OfType<TextBox>().Any(x => string.IsNullOrWhiteSpace(x.Text));

     if(result==true) MessageBox.Show("Please do not leave a textbox blank.");

     return !result;
}
private bool EmptyTextBox()
{
bool result=this.Controls.OfType().Any(x=>string.IsNullOrWhiteSpace(x.Text));
if(result==true)MessageBox.Show(“请不要将文本框留空”);
返回!结果;
}
试试这个:

你快到了

if (!Controls.OfType<TextBox>().Any(textBox => 
String.IsNullOrWhiteSpace(textBox.Text))) return false;
if(!Controls.OfType().Any(textBox=>
String.IsNullOrWhiteSpace(textBox.Text)))返回false;
OfType-仅返回您提供的类型的元素

强制转换-将尝试将所有元素强制转换为您提供的类型

你快到了

if (!Controls.OfType<TextBox>().Any(textBox => 
String.IsNullOrWhiteSpace(textBox.Text))) return false;
if(!Controls.OfType().Any(textBox=>
String.IsNullOrWhiteSpace(textBox.Text)))返回false;
OfType-仅返回您提供的类型的元素

强制转换-将尝试将所有元素强制转换为您提供的类型

你快到了

if (!Controls.OfType<TextBox>().Any(textBox => 
String.IsNullOrWhiteSpace(textBox.Text))) return false;
if(!Controls.OfType().Any(textBox=>
String.IsNullOrWhiteSpace(textBox.Text)))返回false;
OfType-仅返回元素