How can I check multiple textboxes if null or empty without a unique test for each?

前端 未结 2 813
既然无缘
既然无缘 2020-12-08 23:53

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 no

相关标签:
2条回答
  • 2020-12-09 00:06

    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.
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-09 00:27

    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
    }
    
    0 讨论(0)
提交回复
热议问题