• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 204
  • Last Modified:

Check if object still exists

I've got a main dialog, which itself creates new dialogs. Every time I create a new dialog, I do this:

NoteForm note = new NoteForm();
noteObjects.Add(note);
note.Show();

That means that I save a reference to the dialog in an ArrayList called noteObjects. The dialogs can be minimized and hidden. I added a button in my main dialog that shows all dialogs. So what I do is iterating through the ArrayList and call:

for(i=0;i<noteObjects.Count;i++) {
((NoteForm)noteObjects[i]).Show();
}

If a dialog has been closed, there are invalid entries in the array which cause a System.ObjectDisposedException

How can I remove array elements that have become invalid?
if ((NoteForm)noteObjects[i] == null) ... doesn't work.

Thanks for any suggestion,
Martin
0
marb
Asked:
marb
1 Solution
 
AlexFMCommented:
Subscribe to Closed event for each created form. In this event handler remove sender from noteObjects list.
0
 
marbAuthor Commented:
I've added this line in my main dialog:

note.Closing += new System.ComponentModel.CancelEventHandler(this.NoteFormClosing);

and added a event handler:

void NoteFormClosing(object sender, System.ComponentModel.CancelEventArgs e)
{
  noteObjects.Remove(sender);
}

It works fine. Thank you AlexFM!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now