I think you have a confusion between form classes and instances. When you say that something is shown "twice", it may only mean that you have two instances of the same class at the same time. If you need to avoid it, you should work with only one instance. There are a number of ways to do it, but the best and most universal approach here is using the
lazy pattern, in it simplest form:
partial class MyMainForm {
MyChartForm chartForm;
void ShowChart() {
if (chartForm == null) {
chartForm = new MyChartForm();
chartForm.Owner = this;
chartForm.ShowInTaskbar = false;
}
chartForm.ShowDialog();
}
}
One benefit of this approach is that you are not risking of creating a form too early, when some of the operations would be invalid. For
System.Windows.Forms
, this is not a big problem, but with WPF, it can really save a day.
This technique will require something else if you want to have a non-modal additional form. [EDIT] Such situation appears when
Form.Show
method is used instead of
Form.ShowDialog
. [END EDIT]
In this case, I would advise you to prevent closing (as you won't be able to show it again because of disposed objects; you need to hide it on
FormClosing
and cancel closing (this event can be canceled; the event argument type has
Cancel
Boolean member; you will need to set it to true), to show the form again later.
Please see also:
http://msdn.microsoft.com/en-us/library/system.componentmodel.canceleventargs.aspx[
^],
http://en.wikipedia.org/wiki/Lazy_initialization[
^].
—SA