Deleting an instance of a control

  • Thread starter Thread starter Peter Webb
  • Start date Start date
P

Peter Webb

I have largely written a C# program to play a word guessing game.

Users get several tries at guessing a word; I want to display the sequences
of guesses and information about the guesses by adding stuff to the form. So
a record of guesses is kept on screen.

Because I thought I could, I created a class to hold all the information
about each guess, inherited from Panel to hold the guesses, and then added
the control dynamically as each guess is made.

Its:

Class cwBox: Panel
{// bunch of extra properties and methods to set up the appearance
}

I can instantiate this, and add it to the Form1 with

cwBox nextcwBox = new cwBox();
Controls.Add(nextcwBox);
currentcwBox=nextcwBox; // where currentcwBox is a global pointer to the
last created control

When I started, I thought it would be dead easy to remove them from the loop
of controls associated with Form1, and by killing any reference to them
would allow the GC to clean it all up.

I was hoping that:

currentcwBox=null;

and then some line which simply removed every instance of cwBox from the
Collection of Controls on the form.

Something like:

Form1.Controls.oftype<cwBox>.Remove();

would be ideal, but of course is wrong. I have tried lots of various ways of
simply removing all instances of cwBox and all references to those
instances, but cannot.

Unless somebody tells me the magic string, I will have to re-write the damn
thing as an array of Controls.
 
Peter Webb said:
I have largely written a C# program to play a word guessing game.

Users get several tries at guessing a word; I want to display the
sequences of guesses and information about the guesses by adding stuff to
the form. So a record of guesses is kept on screen.

Because I thought I could, I created a class to hold all the information
about each guess, inherited from Panel to hold the guesses, and then added
the control dynamically as each guess is made.

Its:

Class cwBox: Panel
{// bunch of extra properties and methods to set up the appearance
}

I can instantiate this, and add it to the Form1 with

cwBox nextcwBox = new cwBox();
Controls.Add(nextcwBox);
currentcwBox=nextcwBox; // where currentcwBox is a global pointer to the
last created control

When I started, I thought it would be dead easy to remove them from the
loop of controls associated with Form1, and by killing any reference to
them would allow the GC to clean it all up.

I was hoping that:

currentcwBox=null;

and then some line which simply removed every instance of cwBox from the
Collection of Controls on the form.

Something like:

Form1.Controls.oftype<cwBox>.Remove();

would be ideal, but of course is wrong. I have tried lots of various ways
of simply removing all instances of cwBox and all references to those
instances, but cannot.

Unless somebody tells me the magic string, I will have to re-write the
damn thing as an array of Controls.


Have you tried:

this.Controls.Remove(currentcwBox);
currentcwBox.Dispose();
 
Back
Top