How to check for empty textbox

Sannyi97 picture Sannyi97 · Jul 3, 2014 · Viewed 33.5k times · Source

On a site I found the TryParse method (how to check if there is a empty textbox in C#) but I don't know how to use it.

int outputValue=0;
bool isNumber=false;
isNumber=int.TryParse(textBox1.Text, out outputValue); 
if(!isNumber)
{
 MessageBox.Show("Type numbers in the textboxes");
}
else
{
// some code
}

and how can i solve this for 1+ number of textboxes

Answer

Arun Chandran C picture Arun Chandran C · Jul 3, 2014

If you want to check empty for all text box control in your page .Try IsNullOrWhiteSpace

 foreach (Control child in this.Controls)
    {
        TextBox textBox = child as TextBox;
        if (textBox != null)
        {
            if (!string.IsNullOrWhiteSpace(textBox.Text))
            {
                MessageBox.Show("Text box can't be empty");
            }
        }
    }