Hi, I am having problem with a particular process of commands. My program begins with detecting whether a textbox is empty at the press of a button and if it is it will display an error code which i scripted to do, you can create up to 10 textboxes which is where i have the problem because i need to check each runtime created textbox to see if it is empty, the first problem i have is that i get one message box displayed per empty textbox, this is the code i am using.

foreach (Control ctrl in panel1.Controls)
            {
                 TextBox tb = ctrl as TextBox;
                {
                    if (tb != null)
                    {


                        if (!string.IsNullOrEmpty(tb.Text))
                        {
                            Form2 form2 = new Form2();
                            form2.Show();
                        }
                        else
                        {
                            MessageBox.Show("Error1, Please Enter Areas In Numeric Area And Fill In All Text Boxes");
                        }
                    }

As you can see if the textbox has something in it form2 will open which is the problem because if up to 10 textboxes have writing in it which is my goal then 10 form2's will open. Can anyone help me with this problem please?

Do you only want one Form2 to open? Or do you want to Form2 to be shown modally...? What is the desired behavior?

whats the diffrence? sorry but i'm fairly new to c#

What do you want to happen? Do you want one Form2 to open once if a text box is empty?

yes just one form2 to open if all the textboxes on screen have something in at the moment its opening one for each textbox with something in.

if you dont want to display more than 1 Form2
keep following code out side the foreach statement:

Form2 form2 = new Form2();
                            form2.Show();

OK I tried to make this code very clear and readable for you. See if this is what you want:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace daniweb
{
  public partial class Form1 : Form
  {
    public Form1()
    {
      InitializeComponent();
    }

    private bool AreAllTextBoxesFilledIn()
    {
      foreach (Control ctrl in panel1.Controls)
      {
        TextBox tb = (ctrl as TextBox);
        if (tb == null)
          continue; //The control wasn't a textbox

        if (string.IsNullOrEmpty(tb.Text.Trim()))
        {
          //The text box is empty. Lets focus the text box so they know
          //which one we are talking about.
          tb.Focus();

          return false; //break out because they need to fill it in
        }
      }
      return true; //all the text boxes are filled in
    }

    private void button1_Click(object sender, EventArgs e)
    {
      if (AreAllTextBoxesFilledIn() == true)
      {
        Form2 form = new Form2();
        form.Show();
      }
      else
      {
        MessageBox.Show("Please fill in all 10 text boxes. The text box has been focused for you");
      }
    }
  }
}

Thank you for your kind help, that works perfectley.

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.