C# Remove control that has not yet been added?

Your code creates the controls but does not add them to the form.

Your code creates the controls but does not add them to the form. The controls will not be added to your form until you add them to your form's Controls collection (or a Controls collection of a child control). Since you haven't added them to the form, you can't remove them from the form.

And it would make no sense if you could. Just don't add them to the form if you don't need them. Or, better yet, just don't create them in the first place.

You don't have to do anything. Once the object falls out of scope and is not referenced then it will be eligible for garbage collection and will be disposed at some point. However, it would be a much better design if you didn't create the controls you don't need in the first place.

What you name you give an object is independent of the name of the variable you use to hold a reference to it.

You will not be able to remove these controls visibly as these controls are not a part of your windows from. Hence only possible way is set the variable to null field = null; field3 = null; hope this will work for you; if these controls already in the windows forms you can use the Controls. Remove("NameOfTheControl"); or just ignore them and continue your work garbage collector will do this job for you.

Thanks for your comments. Turns out this isnt going to be as easy as hoped. – Daniel Hobbs Jun 25 '11 at 21:33.

As mentioned from others you first need to add them to a controll collection before you can remove them from a controll collection :-) also it is possible to trigger the garbagecollector to 'destroy' your objects: garbagecollector.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions