By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,018 Members | 1,204 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,018 IT Pros & Developers. It's quick & easy.

Form Events - Closing, Disposing - Removing from collections when closed

P: n/a
Hi,

I have a ChatFormCollection class which holds a collection of the class
ChatForm which inherits from Form.
MainForm has an instance of ChatFormCollection.
When a ChatForm instance is created it is added to the ChatFormCollection of
MainForm.
this.parent.ChatForms.Add(this);

This works fine, but I've always found the removing hard.
Because the form has to be removed from the collection when the user closes
the form, but not when he minimizes the form.

I tryed the following code

private void ChatForm_Closed(object sender, System.EventArgs e)
{
this.parent.ChatForms.Remove(this);
}

but apparently the form never gets removed from the collection
and when the following code is called again:

int index = this.chatForms.IndexOf((LmimUser)this.lstContacts. SelectedItem);

if(index < 0)
index = this.chatForms.Add(new
ChatForm(this,(LmimUser)this.lstContacts.SelectedI tem));

this.chatForms[index].Show();
this.chatForms[index].BringToFront();
this.chatForms[index].Activate();
this.chatForms[index].Focus();

I get the following exception (while the form it is trying to access should
actually have been removed from the collection
and therefore a new form should have been added to the collection.)

An unhandled exception of type 'System.ObjectDisposedException' occurred in
system.windows.forms.dll
Additional information: Cannot access a disposed object named "ChatForm"

Can someone help me with this?

Thnx.

Timothy.


---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.524 / Virus Database: 321 - Release Date: 6/10/2003
Jul 21 '05 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.