Click here to Skip to main content
15,896,313 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In my Windows Application, there is a form called Order Details in which I used many textboxes and comboboxes to input data. Whenever the user will click the Add button it should prompt the user if there will be a blank textbox if the user left it blank. How is it possible?
I dont know more. Please help me.
Posted
Comments
thatraja 12-Oct-13 2:29am    
Google for "C# Validate empty textboxes"

you should use if else statemet for this

C#
if(textbox1.text!="" && textbox2.text!="" or so no...)
{
your code.....
}
else
{
message.show("Please fill full info ");

}
 
Share this answer
 
v2
Try in this way;

private bool WithErrors()
{
    if(textBox1.Text.Trim() == String.Empty) 
        return true; // Returns true if no input or only space is found
    if(textBox2.Text.Trim() == String.Empty)
        return true;
    // Other textBoxes.

    return false;
}

private void buttonSubmit_Click(object sender, EventArgs e)
{
    if(WithErrors())
    {
        // Notify user for error.
    }
    else
    {
        // Do whatever here... Submit
    }
}
 
Share this answer
 
I'm assuming you don't wanna check the textboxes individually, like

C#
//C# 4.0
if (String.IsNullOrWhiteSpace(textbox.Text)) 
{
//The textbox is empty.
MessageBox.Show("You have empty fields in the form");
}

//C# < 4.0
if (String.IsNullOrEmpty(textbox.Text.Trim())
{
//The textbox is empty.
MessageBox.Show("You have empty fields in the form");
}


That allows you to check a specific control and report on it.

But when you have too many textbox controls, you can do this

C#
foreach (Control control in this.Controls)
{
    if (control is TextBox && String.IsNullOrWhiteSpace(((TextBox)control).Text))
    {
        //This control is empty. //Do whatever you want here.
        return;
    }
    //You can even check if there are unchecked CheckBoxes or Empty Comboboxes
    if (control is CheckBox && !((CheckBox)control).Checked)
    {
       //This Checkbox is uncheked.
       return;
    }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900