M
Martin Douglas
Hey guys, maybe someone can help me with some MDI issues I have. A
co-worker asked me a very simple question, one that I blew off as trivial,
and it has become a time-consuming issue.
Simply put, there is an MDIForm and a child form.
The MDIForm does the usual from within its constructor...
this.IsMdiContainer = true;
Form2 frm = new Form2();
frm.MdiParent = this;
frm.Show();
Okay, so nothing special there.
The child form then has a combobox on it. The question was, how do I get my
child form to close if I pick a particular item from the combobox. Or, to
make it easier, close the child form when the item selection changes in the
combobox.
So the following is in the child form...
private void comboBox1_SelectedIndexChanged(object sender,
System.EventArgs e)
{
this.Close();
}
Unfortunately this throws an exception indicating that the combobox has
already disposed itself and a null-reference exception is raised.
So I suggested the following...
private void comboBox1_SelectedIndexChanged(object sender,
System.EventArgs e)
{
this.Dispose(false);
}
This gets rid of the exception, but the child form is really not disposed,
even though visually it looks like it has gone away. It can be made to come
back sporadically by clicking inside the MDI parent form.
Using a true argument in the Dispose yields the same null pointer exception.
So with that, my understanding of forms went down the tubes, along with my
understanding of how the GC works and how correct implementations of
IDisposable work.
Can someone comment on how to get an MDI child form to close itself (i.e.
commit suicide)? Closing the child from a menu or toolbar on the parent
form is not a problem.
Thanks in advance.
MD
co-worker asked me a very simple question, one that I blew off as trivial,
and it has become a time-consuming issue.
Simply put, there is an MDIForm and a child form.
The MDIForm does the usual from within its constructor...
this.IsMdiContainer = true;
Form2 frm = new Form2();
frm.MdiParent = this;
frm.Show();
Okay, so nothing special there.
The child form then has a combobox on it. The question was, how do I get my
child form to close if I pick a particular item from the combobox. Or, to
make it easier, close the child form when the item selection changes in the
combobox.
So the following is in the child form...
private void comboBox1_SelectedIndexChanged(object sender,
System.EventArgs e)
{
this.Close();
}
Unfortunately this throws an exception indicating that the combobox has
already disposed itself and a null-reference exception is raised.
So I suggested the following...
private void comboBox1_SelectedIndexChanged(object sender,
System.EventArgs e)
{
this.Dispose(false);
}
This gets rid of the exception, but the child form is really not disposed,
even though visually it looks like it has gone away. It can be made to come
back sporadically by clicking inside the MDI parent form.
Using a true argument in the Dispose yields the same null pointer exception.
So with that, my understanding of forms went down the tubes, along with my
understanding of how the GC works and how correct implementations of
IDisposable work.
Can someone comment on how to get an MDI child form to close itself (i.e.
commit suicide)? Closing the child from a menu or toolbar on the parent
form is not a problem.
Thanks in advance.
MD