Click here to Skip to main content
15,888,968 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello,

I have a menu in which there are menu items. On the click of these menu items, I am displaying grids, charts which are placed on separate forms. Now once I open a chart/Grid, I donot want the chart/Grid to open again on menu item click, i.e I just want them to open once(keep in mind, I want to open a chart and a grid form simultaneously, this rules out the frm.ShowDialog()option).

My question is how do I restrict the application to display the forms just once. I can have two separate forms but not the same one.

Please help me on this. MY application is a windows based application

Thanks
Posted
Comments
Sergey Alexandrovich Kryukov 15-Oct-12 17:11pm    
I removed all previous comments which were nothing but a random confusion... :-)
--SA

1 solution

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:
C#
partial class MyMainForm {

    MyChartForm chartForm; // at first, it's null; will remain null before first use

    void ShowChart() { //add "internal" if you want to use it from outside the form
       if (chartForm == null) {
           chartForm = new MyChartForm();
           chartForm.Owner = this;
           chartForm.ShowInTaskbar = false; // important for application integrity
       } //if chartForm == null
       chartForm.ShowDialog();
    } //ShowChart

} //class MyMainForm

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
 
Share this answer
 
v8
Comments
GB_1114 15-Oct-12 16:41pm    
Hi Sergey ,

Thanks for the reply,
Can I use chartForm.Show() instead of showdialog as it wouldnt enable me to open other forms..
Anyways I would implement this

Thanks a lot
GB
Sergey Alexandrovich Kryukov 15-Oct-12 16:45pm    
You are welcome.

"Instead of ShowDialog" situation is described in the paragraph "This technique will require something else...". It's called "non-modal" and show dialog is "modal".

Anyway, if you still see the problem, don't hesitate to ask a follow-up question by just commenting on this answer.
But if you already got it, please accept it formally (green button).
--SA
Sergey Alexandrovich Kryukov 15-Oct-12 16:47pm    
As you accepted it formally, I assume you got it. This is great. (But my invitation to ask some further question is still in order.)
Good luck, call again.
--SA
fjdiewornncalwe 15-Oct-12 16:45pm    
+5. Excellent. The only change I would suggest is to use the .Show() instead of .ShowDialog() as the OP says he potentially needs to open up to 2 different forms simultaneously in which case the ShowDialog won't help.
Sergey Alexandrovich Kryukov 15-Oct-12 16:46pm    
Thank you, Marcus. I actually covered this possibility, too, in brief -- please see my comment above.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900