'best way to write code of bool that check 6 functions?

I have this code:

          bool validInput = !string.IsNullOrWhiteSpace(reg_name_tbx.Text)
               && !string.IsNullOrWhiteSpace(reg_adr_tbx.Text)
           && !string.IsNullOrWhiteSpace(reg_phn_tbx.Text)
           && !string.IsNullOrWhiteSpace(reg_pwr_tbx.Text)
           && !string.IsNullOrWhiteSpace(reg_email_tbx.Text)
           && !string.IsNullOrWhiteSpace(reg_type_cbx.Text);

Is there a better way to write this? It is checking that all text boxes have valid input from the user..



Solution 1:[1]

If the textboxes are all on the form:

this.Controls.OfType<TextBox>().Any(tb => string.IsNullOrWhiteSpace(tb.Text))

If they're in a panel, replace this with the name of the panel

If you have other textboxes that shouldn't be included you can add some other condition that reduces the candidate textboxes to just those you're interested in; perhaps:

 this.Controls.OfType<TextBox>().Any(tb => tb.Name.StartsWith("reg") && string.IsNullOrWhiteSpace(tb.Text))

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1