Hi, Is it at all possible to detect if a user has inputed text into a text box for example.

if (textbox1.Text == ("detect if text is in textbox")
{
// do something
}

Recommended Answers

All 7 Replies

Use static variable and TextChange event.

you can also use:

if (!string.IsNullOrEmpty(textBox1.Text))
{
  //Do something
}
commented: Great Responce +1

This is what i am trying to do

private void button2_Click_1(object sender, EventArgs e)
        {
            FieldStore();
            
            foreach (Control c in panel1.Controls)
            {

                    if (!string.IsNullOrEmpty(c.Name = "field1"))
                    {
                        MessageBox.Show("Please Enter At Least One Room Or Put Your Rooms In Numeric Order");
                    }
                   
            }
        }

and so far messageboxes are coming up no matter what is in the dynamically created textboxes, i only want it to pop up if there is something in the textbox.

Yeah .. you have a bug in your code that will cause it to never evaluate the condition as true.

Try this:

private void button1_Click(object sender, EventArgs e)
    {
      foreach (Control ctrl in panel1.Controls)
      {
        TextBox tb = (ctrl as TextBox);
        if (tb != null)
        {
          if (!string.IsNullOrEmpty(tb.Text))
          {
            tb.Focus();
            MessageBox.Show("Please dont type in the text boxes");
            tb.Focus();
            break;
          }
        }
      }
    }

thanks thant helps alot but now i'm trying this...

private void button2_Click_1(object sender, EventArgs e)
        {
            FieldStore();
            
            foreach (Control ctrl in panel1.Controls)
            {
                 TextBox tb = ctrl as TextBox;
                {
                    if (tb != null)
                    {
                        MessageBox.Show("Error1, Please Enter Areas In Numeric Area And Fill In All Text Boxes");
                        
                        if(!string.IsNullOrEmpty (tb.Text))
                        {
                            Form2 form2 = new Form2();
                            form2.Show();
                        }
                    }
                }
                
              
                   
            }
        }

but its displaying the error message even if there is text in text box, any ideas?

You put the message box in the wrong place.

if (tb != null)
                    {  
                        if(!string.IsNullOrEmpty(tb.Text))
                        {
                            Form2 form2 = new Form2();
                            form2.Show();
                        }
                        else
                       {
                          //error message goes here...
                       }
                    }
commented: Ecellent +1

Thanks sooo much!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.