Click here to Skip to main content
15,888,521 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,


I have a Menustrip1 With 1Menu Item Say New.

When i click on New Menu Item assume Form1 is opend.

My Problem is When i click on New for 5 Times. it is opening Form1 5 Times.

How to open Form1 Once only even though i clicked for N Times.

Any help would be really appreciated.

Regards,
Pawan.
Posted

You don't mention if you application is SDI or MDI, and there are different ways of solving this....

1) SDI: Create a Form1 myForm = null; variable on form level in the form where you have the New menu item code. When the menu item is clicked, you do:

if (myForm == null)
{
   myForm = new Form1();
   myForm.Show();
}


2) SDI: You can use a boolean variable to keep track of Form1, when you open it, you set form1IsOpen = true;. But then you need to raise an event in the form where you have your menu from Form1 when it's closing so that you can reset the variable to false.

3) MDI: You need to loop through the open MDI child forms and check if their class is Form1.
 
Share this answer
 
v2
Comments
Pawan Kiran 12-Jul-10 8:45am    
Reason for my vote of 5
it solves my issue 90% thank u so much.
Johnny J. 12-Jul-10 9:21am    
:) What's the last 10%???
Hi,
Another option it to Define a Shared(Static) property in the form which always to points its object.
we can wrap the logic
C#
if (myForm == null)
{
   myForm = new Form1();
   myForm.Show();
}
with in this constructor

that is

Class Class1

Shared ReadOnly Property Instance
   Get
    IF _instace is nothing then 
     instace  =New Class1
    End if
   End 
End Class
 
Share this answer
 
Please refer to one of my previous solutions: how to open only one instance of class not more than that ?[^].

Worked before and will also work for you!

-MRB
 
Share this answer
 
See this[^] article.
 
Share this answer
 
Comments
Johnny J. 12-Jul-10 8:03am    
Reason for my vote of 1
Not applicable to this situation - It's a good idea to actually read the question before answering it. This is not a question about making a single instance application, but on how to make a FORM in an application single instance...

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