Link to home
Start Free TrialLog in
Avatar of imrancs
imrancsFlag for Pakistan

asked on

How to prevent opening of new windows(or forms) at runtime when OpenNewForm(newItemForm etc) button is clicked.

Can any one tell me how to prevent to open many windows(or forms) when i click on OpenNewForm(newItemForm etc) button repeatedly, i want, only one window(or form ) should open  although i click it several times.
Avatar of lizard450

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

k...if Form1 is the parent form from where all ur windows are called what u could do is

declare all the Form in the form1 class like

public class Form1:S.W.Form
Form2 MyForm2;
Form3 MyForm3;
Form4 MyFrom4l

private void button1_click(object sender,System.Eventargs e)
if(Myfor2 == null || MyForm2.IsDisposed)
MyForm2 = new Form2();
//Similarly for all other forms
if ur forms access is spread throyughout the app then the best way would be to

maintain a list of all the open forms ..(add ur forms to an Hastable or something every time) and before opening a form check whether the form exists in the HashTable if so get the object from the table and show()
Actually gauthampj I disagree if access is spread throughout the app the best way is to implement the pattern I pointed to above.  This way you don't have to "maintain" anything it all just works.  

However, if Form1 is the parent object then I would agree with you.