I've been working on this application for a weeks now, and have become very excited about its release.
However, I've run into a VERY serious problem.
I'll paste a bit of code, and explain afterwards.
mainframe = new Panel();
mainframe.Parent = this;
mainframe.Location = new Point(125, 50);
mainframe.Size = new Size(650 - mainframe.Location.X, 500 - mainframe.Location.Y);
mainframe.Controls.Add(new Pages.MainPage());
memorize_menubutton = new Buttons.MenuButton("Memorize", ContentAlignment.MiddleRight);
memorize_menubutton.Parent = this;
memorize_menubutton.Location = new Point(0, 60);
memorize_menubutton.Click += new EventHandler(this.memorize_menubutton_click);
connect_menubutton = new Buttons.MenuButton("Connect", ContentAlignment.MiddleCenter);
connect_menubutton.Parent = this;
connect_menubutton.Location = new Point(0, 90);
connect_menubutton.Click += new EventHandler(this.connect_menubutton_click);
header = new Header();
header.Parent = this;
header.Location = new Point(0, 0);
//header.Font = new Font("Times New Roman", 20, FontStyle.Bold, GraphicsUnit.Pixel);
header.MouseDown += new MouseEventHandler(this.moveform_mousedown);
header.MouseUp += new MouseEventHandler(this.moveform_mouseup);
header.MouseMove += new MouseEventHandler(this.moveform_mousemove);
// <SUBCONTROLS>
destroy = new Destroy();
destroy.Parent = header;
destroy.Size = new Size(15, 15);
destroy.Click += new EventHandler(this.destroy_click);
// </SUBCONTROLS>
private void memorize_menubutton_click(object sender, EventArgs e) {
mainframe.Controls.Clear();
mainframe.Controls.Add(new Pages.QuizPage(questions.TheBook));
}
private void connect_menubutton_click(object sender, EventArgs e) {
mainframe.Controls.Clear();
mainframe.Controls.Add(new Pages.MainPage());
}
private void destroy_click(object sender, EventArgs e) {
this.Close();
}
Ok let's walk thru it really fast.
mainframe is a PANEL control, that will hold what I call "pages", but what they are is actually controls derived from the PANEL control.
There are two "MenuButton", they are actually LABEL controls, that I have turned into buttons by the click event.
The HEADER control is a LABEL.
The DESTROY control is a LABEL control.
The DESTROY control is what shuts down the app, by the onclick event.
Now, the two MENUBUTTONS, all they obviously do is make the main PANEL control get new controls in it! It has NOTHING to do with DESTROY object.
I hit the DESTROY object, and the form shuts down. Just like always! Perfect!
BUT, when I hit those MENUBUTTONs three time or MORE, if I hit the DESTROY object afterwards, NOTHING happens! As if there was new CLICK EVENT specified.
WEIRD!
Note I said three or more! I can hit them once or twice, and then hit the DESTROY object and it closes properly. But three or more hits, the destroy object seems to lose it's CLICK event handler!!!!! WHAT'S UP WITH THIS !
This has caused very much concern! I've been really excited about C# and .NET. But... I see no explanation for this strange effect, and therefore can't be expected to create large applications in .NET!
Can somebody tell me why this happened Can this be fixed

Help! - something WEIRD happens!
amonteiro
I don't know about the rest, but why would a collection call the Dispose() method of its items when you Clear() it The collection doesn't know if those objects are still used somewhere else... That's the GC's work.
AbhijitB
It's me, I'm giving you all an update... I was thinking, and thinking, and doing some more thinking, trying to figure out why this is happening. (of course, I was drinking coffee at the same time) Somehow, I always think of solutions while drinking coffee... interesting huh
Anyways!
Here's what I started doing first.
First, I thought I'd derive this DESTROY object from the BUTTON control rather than that LABEL control, thinking that perhaps the BUTTON control might somehow, being a button, tell the computer "HEY! I'm a button! I'm important! Don't forget me!"
I compiled and ran it, but got the same effect. It lasted much longer though! But in the end, it forgot it's CLICK even handler. But I got to switch the "pages" around a LOT before it "forgot" it's click event.
I was then stumped...
Then I noticed this in my code:
mainframe.Controls.Clear();
I wondered, WHAT IF, this CLEAR method doesn't call the DISPOSE method of all the objects that it contains ! And therefore, leaving all those objects (each time the buttons are pressed) in memory... and maybe by filling up the memory like that, this "managed code" becomes forgetful And forgets other object click events ! I dunno... that's pretty far out isn't it
Well, anyways... I then replaced that line of code with this instead:
mainframe.Controls[0].Dispose();
I ran the application, and it works... (SO FAR) I keep pressing those buttons for as long as I want, switching the "pages" around... all works, I hit the DESTROY object and the form closes. Good. I then derived my control from the LABEL again, like I originally did, and it all still works.
So, it appears my theory was correct
Could one of the top posters here that knows everything there is to know at LEAST give me an explanation as to why this happened, and why it's fixed now Maybe there's some things about computers that can't be explained, I don't know...
But about my theory, is that even possible If you fill up the memory with useless objects, that it may "forget" click events (or maybe even properties) of other objects
That's the only explanation that *I (with my limited thinking abilities [ha ha]) can come up with as to why this line made it work: mainframe.Controls[0].Dispose();
Thoughts anyone
By the way... It's my guess that the CLEAR() method of the System.Windows.Forms.Control.ControlCollection does NOT call the Dispose() method of the objects it contains. (Which is what I assumed)
Am I correct
JMDavis