如果没有对每个文本框进行唯一的测试,如何检查多个文本框是否为空或为空?

发布于 2024-12-24 21:03:04 字数 1274 浏览 0 评论 0原文

我的表单上有大约 20 个文本字段可供用户填写。如果用户在任何文本框中输入了任何内容,我想提示用户考虑保存。现在的测试真的很长而且很混乱:

if(string.IsNullOrEmpty(txtbxAfterPic.Text) || string.IsNullOrEmpty(txtbxBeforePic.Text) ||
            string.IsNullOrEmpty(splitContainer1.Panel2) ||...//many more tests

有没有一种方法可以使用类似任何数组的东西,其中数组由文本框组成,然后我以这种方式检查它?还有哪些其他方法可能是一种非常方便的方法来查看自程序启动以来是否进行了任何更改?

我应该提到的另一件事是有一个日期时间选择器。我不知道是否需要对此进行测试,因为日期时间选择器永远不会为空或为空。

编辑: 我将答案合并到我的程序中,但我似乎无法使其正常工作。 我如下设置测试并继续触发 Application.Exit() 调用。

        //it starts out saying everything is empty
        bool allfieldsempty = true;

        foreach(Control c in this.Controls)
        {
            //checks if its a textbox, and if it is, is it null or empty
            if(this.Controls.OfType<TextBox>().Any(t => string.IsNullOrEmpty(t.Text)))
            {
                //this means soemthing was in a box
               allfieldsempty = false;
               break;
            }
        }

        if (allfieldsempty == false)
        {
            MessageBox.Show("Consider saving.");
        }
        else //this means nothings new in the form so we can close it
        {                
            Application.Exit();
        }

为什么根据上面的代码在我的文本框中找不到任何文本?

I have about 20 text fields on a form that a user can fill out. I want to prompt the user to consider saving if they have anything typed into any of the text boxes. Right now the test for that is really long and messy:

if(string.IsNullOrEmpty(txtbxAfterPic.Text) || string.IsNullOrEmpty(txtbxBeforePic.Text) ||
            string.IsNullOrEmpty(splitContainer1.Panel2) ||...//many more tests

Is there a way I could use something like an Array of any, where the array is made of the text boxes and I check it that way? What other ways might be a very convenient way in which to see if any changes have been made since the program started?

One other thing I should mention is there is a date time picker. I don't know if I need to test around that as the datetimepicker will never be null or empty.

EDIT:
I incorporated the answers into my program, but I can't seem to make it work correctly.
I set up the tests as below and keep triggering the Application.Exit() call.

        //it starts out saying everything is empty
        bool allfieldsempty = true;

        foreach(Control c in this.Controls)
        {
            //checks if its a textbox, and if it is, is it null or empty
            if(this.Controls.OfType<TextBox>().Any(t => string.IsNullOrEmpty(t.Text)))
            {
                //this means soemthing was in a box
               allfieldsempty = false;
               break;
            }
        }

        if (allfieldsempty == false)
        {
            MessageBox.Show("Consider saving.");
        }
        else //this means nothings new in the form so we can close it
        {                
            Application.Exit();
        }

Why is it not finding any text in my text boxes based on the code above?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

剪不断理还乱 2024-12-31 21:03:04

当然 - 枚举您的控件以查找文本框:

foreach (Control c in this.Controls)
{
    if (c is TextBox)
    {
        TextBox textBox = c as TextBox;
        if (textBox.Text == string.Empty)
        {
            // Text box is empty.
            // You COULD store information about this textbox is it's tag.
        }
    }
}

Sure -- enumerate through your controls looking for text boxes:

foreach (Control c in this.Controls)
{
    if (c is TextBox)
    {
        TextBox textBox = c as TextBox;
        if (textBox.Text == string.Empty)
        {
            // Text box is empty.
            // You COULD store information about this textbox is it's tag.
        }
    }
}
猛虎独行 2024-12-31 21:03:04

基于 George 的答案,但使用一些方便的 LINQ 方法:

if(this.Controls.OfType<TextBox>().Any(t => string.IsNullOrEmpty(t.Text)))  
{
//Your textbox is empty
}

Building on George's answer, but making use of some handy LINQ methods:

if(this.Controls.OfType<TextBox>().Any(t => string.IsNullOrEmpty(t.Text)))  
{
//Your textbox is empty
}
油饼 2024-12-31 21:03:04
public void YourFunction(object sender, EventArgs e) {
    string[] txtBoxArr = { textBoxOne.Text, textBoxTwo.Text, textBoxThree.Text };
    string[] lblBoxArr = { "textBoxOneLabel", "textBoxTwoLabel", "textBoxThreeLabel" };
    TextBox[] arr = { textBoxOne, textBoxTwo, textBoxThree };

    for (int i = 0; i < txtBoxArr.Length; i++)
    {
        if (string.IsNullOrWhiteSpace(txtBoxArr[i]))
        {
            MessageBox.Show(lblBoxArr[i] + " cannot be empty.");
            arr[i].Focus();
            return;
        }
    }
}
public void YourFunction(object sender, EventArgs e) {
    string[] txtBoxArr = { textBoxOne.Text, textBoxTwo.Text, textBoxThree.Text };
    string[] lblBoxArr = { "textBoxOneLabel", "textBoxTwoLabel", "textBoxThreeLabel" };
    TextBox[] arr = { textBoxOne, textBoxTwo, textBoxThree };

    for (int i = 0; i < txtBoxArr.Length; i++)
    {
        if (string.IsNullOrWhiteSpace(txtBoxArr[i]))
        {
            MessageBox.Show(lblBoxArr[i] + " cannot be empty.");
            arr[i].Focus();
            return;
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文