Link to home
Start Free TrialLog in
Avatar of marb
marb

asked on

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
ASKER CERTIFIED SOLUTION
Avatar of AlexFM
AlexFM

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of marb
marb

ASKER

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!