How to check if the textbox is empty

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sbandalli
    New Member
    • Feb 2009
    • 53

    How to check if the textbox is empty

    How to check if the text box is empty in C#?? I have to display an error box if the textbox is empty. I am trying this way,but no results. Thank you for the help.

    private void Save_Click(obje ct sender, EventArgs e)
    {

    if (textBox1.Text == String.Empty)
    {

    errortextBox2.V isible = true;
    errorgroupBox2. Visible = true;
    errortextBox3.V isible = true;
    errorbutton1.Vi sible = true;
    }
    else
    {

    string itemToAdd = textBox1.Text;
    listBox1.Items. Add(itemToAdd);
    }
    }
  • sbandalli
    New Member
    • Feb 2009
    • 53

    #2
    I got to know the solution to the problem.Thank you all

    Comment

    • Plater
      Recognized Expert Expert
      • Apr 2007
      • 7872

      #3
      If the textbox is truely empty, that is how to check for it, I suspect your problem might lie elsewhere.
      Is this a web application? Are you remember to check for isPostback in your form_load?

      Comment

      • sbandalli
        New Member
        • Feb 2009
        • 53

        #4
        Hello Plater, Thanks for the reply, I did get it to work. Its a WinForm application...T hanks again...

        Comment

        • Dököll
          Recognized Expert Top Contributor
          • Nov 2006
          • 2379

          #5
          While I am glad you got it working, it would be extremly friendly if posted how you did, for someone else looking, or passing by anyway:-)
          Code:
                      //It's probable adding textbox, checkbox, and other component items to a list will help minimize long if statements, if you have a number of these to do...
                      if (this.txtCustomerID.Text == "")
                      {
          
                          MessageBox.Show("Please enter values in this Customer ID textbox", "Extreme Cinema TextBox Error");
                          this.txtCustomerID.Focus();
                          return;
                      }
          Last edited by Dököll; Dec 5 '10, 11:33 PM. Reason: Adding sample code, and 'this.';-)

          Comment

          Working...