Hi, i am creating up to 10 textboxes using the following code:

if (textBoxIndex != 11)
            {
                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");
            }

What I need to do now is have a button that removes the text boxes from bottom to top, but there may not neccessarily be 10 textboxes present their may only be five for example and I need some help with this please.

Just as the panel1.Controls (wich is of type Control.ControlsCollection) has an Add method, it may not be to suprising to notice, it also has a Remove method.

yes i know that but how do i get it to remove the last one in the sequence and eventually through to the first

To remove the fifth control in your collection use:
panel1.Controls.RemoveAt(4);Use the Count method first to test how many controls are still in the collection.

Count method?

Yes, in case you don't know how many controls are in your collection, you use panel1.Controls.Count;
It would give errors if you removed the fifth control of a collection wich contains only three controls.
There is also the IndexOf method wich returns an integer you could use in the RemoveAt method.
Like in:
panel1.Controls.RemoveAt(IndexOf(field));

ok i see but if i pressed that again it wouldnt remove the text before that, say if i generated 3 textoxes the index would be at 3 and so the code you posted would remove textbox 3 but if i pressed it again it wouldnt remove text box 2 would it?

If you have three controls, the index of the last one is 2!
Remember: your indexes go from 0,1,2, the count is 3!

commented: Excellent +1

ive changed it so that it starts at one so the third textbox would have an index of 3!

oh i get it now, i used the following code to do it.

if (panel1.Controls.Count == 2)
            {
                panel1.Controls.RemoveAt(1);
                textBoxIndex = textBoxIndex - 1;
            }
            if (panel1.Controls.Count == 3)
            {
                panel1.Controls.RemoveAt(2);
                textBoxIndex = textBoxIndex - 1;
            }
            if (panel1.Controls.Count == 4)
            {
                panel1.Controls.RemoveAt(3);
                textBoxIndex = textBoxIndex - 1;
            }
            if (panel1.Controls.Count == 5)
            {
                panel1.Controls.RemoveAt(4);
                textBoxIndex = textBoxIndex - 1;
            }
            if (panel1.Controls.Count == 6)
            {
                panel1.Controls.RemoveAt(5);
                textBoxIndex = textBoxIndex - 1;
            }
            if (panel1.Controls.Count == 7)
            {
                panel1.Controls.RemoveAt(6);
                textBoxIndex = textBoxIndex - 1;
            }
            if (panel1.Controls.Count == 8)
            {
                panel1.Controls.RemoveAt(7);
                textBoxIndex = textBoxIndex - 1;
            }
            if (panel1.Controls.Count == 9)
            {
                panel1.Controls.RemoveAt(8);
                textBoxIndex = textBoxIndex - 1;
            }
            if (panel1.Controls.Count == 10)
            {
                panel1.Controls.RemoveAt(9);
                textBoxIndex = textBoxIndex - 1;
            }
            if (panel1.Controls.Count == 11)
            {
                panel1.Controls.RemoveAt(10);
                textBoxIndex = textBoxIndex - 1;
            }

thanks again!

Although your code will work as you want it.
I have a remark here:
Whenever you see a repetition like you have now, that must start ring a bell in your head:
*** Perhaps I could make a loop here?
*** Perhaps I better make a function?
*** Perhaps .NET has just the thing I need?

I would do it this way:

if (panel1.Controls.Count > 0) //check if I have controls in my collection
{
    panel1.Controls.RemoveAt( panel1.Controls.Count - 1 );
} //this will remove last control in the collection

thanks, i used this

if (textBoxIndex >= 1)
            {
                panel1.Controls.RemoveAt((textBoxIndex) - 1);
                textBoxIndex = textBoxIndex - 1;
            }
            else
            {

            }

thanks for your help

Fine. I'm glad I could help you out.:)

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.