Hello everyone,

First of all, i'm new to C#. I've bought a book to learn the basics and now i'm trying to write a little program.

I'm having problems with removing controls from a form.

Small explanation of the program i'm writing.

I search a folder every 30 seconds to see what files there are inside.

When i find files, i create a label (to display the name) and a button (to open the file). Every time i refresh, i need to clear the existin labels and buttons so they don't mixup.

I do this using a foreach loop:

foreach (Control c in test2.Controls)
{
if (c.Name.Contains("DYNAMIC") == true)
{

test2.Controls.Remove(c);
}
}

I set the name of the label to DYNAMIC_LBL(index) and the buttons to DYNAMIC_BTN(index).

Sometimes it works, sometimes it only deletes the buttons....

Does anyone know what i am doing wrong??

Screenshots below
Name:  firstrefresh.png
Views: 431
Size:  55.8 KBName:  secondrefresh.png
Views: 402
Size:  43.2 KB

Greetings and thanks,

Nico