Hi. I'm new to c# and have ran into a problem, I am using the following code to generate textboxes within a panel.

private void button1_Click(object sender, EventArgs e)
        {
            AddFields();
        }

        private void AddFields()
        {
            if (textBoxIndex != 10)
            {
                TextBox field = new TextBox();
                field.Name = "field" + textBoxIndex.ToString();
                textBoxIndex++;
                field.Size = new Size(200, 20);
                panel1.Controls.Add(field);
            }
            else
            {
                MessageBox.Show("A Maximum of 10 Areas is Supported");
            }

This also caps the textboxes to a maximum of 10, this code works fine for me it is the next bit that I am stuck on. I need to store values entered into each one of the textbox into diffrent variables which I can't do correctly, I am using the following code to find the textbox control.

if (textBoxIndex != 1)
               {
                
               }
               else
               {
                   Control[] ctrls = Controls.Find("field1", false);
                   for (int i = 0; i < ctrls.Length; i++)
                   {
                       TextBox field1 = (TextBox)ctrls[0];
                       field1.Text = Variable1;
                       field1.Text = "";
                   }

               }

I think this is were I have gone wrong and I need help please.

I think you have to look in the Controls collection of panel1, now you are looking in the Controls collection of the form.
So instead of Controls.Find use panel1.Controls.Find

Thanks, it now looks like this.

private void FieldStore()
           {
               if (textBoxIndex != 1)
               {
                  
               }
               else
               {
                   Control[] ctrls = panel1.Controls.Find("field1", false);
                   for (int i = 0; i < ctrls.Length; i++)
                   {
                       TextBox field1 = (TextBox)ctrls[0];
                       field1.Text = Area.Room1;
                       field1.Text = "";
                   }
               }
               
           }

do my if statements look ok, I wasn't sure whether to go != or == I think what I've got is wrong because it's still not working with panel1.Controls.Find.

You don't have to define an array of controls here. You have them ready in your panel1 Controls collection.
Perhaps you should use something like this (I have not tested it, I leave that to you)

string Tboxname = string.Empty();
            for (int i = 0; i < Numberoftextboxes; i++)
            {
                Tboxname = "field" + i.ToString();
                TextBox field1 = panel1.Controls.Find(Tboxname, false);
                field1.Text = Area.Room1;
                field1.Text = "";
            }

Also the last two statements look wierd. You assign a value to Text and then you make Text empty...

No, sorry but whatever i do to that It dosent work, i might add that i'm fairly new to c#...

foreach(Control c in Panel1.Controls)
{
if(c is TextBox)
MessageBox.Show(string.Format("TextBox Name: {0} , TextBox Text {1}",c.Name, c.Text));
}
commented: Nice snippet! +7

Why make it difficult when it is so easy!
Thanks for the clarification Ramy:)

You're welcome my friend, Danny
I Hope it solves their problem.

commented: Brilliant +1

Thanks thats brilliant but it dosent solve my original problem, is their a way to get it specific to one textbox only?

If you know anything about it. Its Text or its Name you can add a condition

foreach(Control c in Panel1.Controls)
{
if(c is TextBox)
if(c.Name = "field1") //or any property c.Text = "" or c.....
MessageBox.Show(string.Format("TextBox Name: {0} , TextBox Text {1}",c.Name, c.Text));
}

wow. thanks thats absolutley amazing but when i try

if(c.Name = "field1")

it comes up with error message "cannot implicitly convert string to bool"
any ideas?

My mistake, modify it to
== instead of =

foreach(Control c in Panel1.Controls)
{
if(c is TextBox)
if(c.Name == "field1") //or any property c.Text == "" or c.....
MessageBox.Show(string.Format("TextBox Name: {0} , TextBox Text {1}",c.Name, c.Text));
}

thanks that helps. but when i try this.

foreach (Control c in panel1.Controls)
               {

                       if (c is TextBox)
                       if (c.Name == "field1")
                           field1.Text = Area.Room1;
                           MessageBox.Show("hi");
               }

It still comes up with field1 does not exist in the form and what i need to do is store field1 into Area.Room1. ideas?

Please attach the project or show us the whole code which creating\searching the TextBox. and don't forget to surround it inside code tag.

this creates the textboxes

private void AddFields()
        {
            if (textBoxIndex != 10)
            {
                TextBox field = new TextBox();
                field.Name = "field" + textBoxIndex.ToString();
                textBoxIndex++;
                field.Size = new Size(200, 20);
                panel1.Controls.Add(field);
            }
            else
            {
                MessageBox.Show("A Maximum of 10 Areas is Supported");
            }

        }

and this is the code that you gave me

private void FieldStore()
           {
               foreach (Control c in panel1.Controls)
               {

                   if (c is TextBox)
                       if (c.Name == "field1")
                           field1.Text = Area.Room1;
                           MessageBox.Show("hi");
               }
            
           }

the error message i get is that field1 cannot be found.

What's this line field1.Text = Area.Room1; ??
Modify it to

private void FieldStore()
           {
               foreach (Control c in panel1.Controls)
               {

                   if (c is TextBox)
                       if (c.Name == "field1")
{
                           c.Text = Area.Room1;
                           MessageBox.Show("This is Field1");
}
               }
            
           }

okay done that, now when i create more then 1 textbox using my above code and run this

private void FieldStore()
           {
               foreach (Control c in panel1.Controls)
               {

                   if (c.Name == "field1")
                           c.Text = Area.Room1;
                           c.Text = "hi";
               }
            
           }

it will say "hi" in all my textboxes not just the one called field1.

Because you didn't take my code and just replace field1 with c, isn't it??
Surround second if with { }
Take my code copy and paste please.

private void FieldStore()
{
foreach (Control c in panel1.Controls)
{
if (c is TextBox)
if (c.Name == "field1")
{
c.Text = Area.Room1;
MessageBox.Show("This is Field1");
}
}
}

ahh okay thats brilliant! problem solved thanks so 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.