Form1 btn1 button Click Show Out Form2, when the Form2 is on, click Btn1 again will not continue to pop up the form, but focus on the open Form2;
When the Form2 is closed, click Btn1 again to show a new Form2;
Form1:
//Create a global collection to place the form objects that have been shownlist<form> list =NewList<form>(); //BTN1 Click event: Private voidButton1_Click (Objectsender, EventArgs e) { BOOLHasform =false;//determine if the form has poppedForm2 F2 =NewForm2 ();//instantiate a popup form objectF2. Owner = This;//set the owner of the popup object to the current form if(list. Count >0)//If there are elements in the collection { foreach(Form FinchList//traverse them { if(F.name = = F2. Name)//if the form object that you want to eject already exists{hasform=true;//indicates that it has poppedF.focus ();//Focus position has popped up the window } } } if(Hasform)//There is currently a window popup{F2. Close (); //release the newly instantiated form object } Else //if no window pops up{list. ADD (F2); //add a new form into the collectionF2. Show ();//pop-up form } }
The form can now be unique, but it cannot pop up again when the form is closed;
You need to write a method in Form1, and call this method in the FormClosing event of the popup window;
//methods in Form1: Public void_remove (Form f) {if(list. Contains (f)) {list. Remove (f); } } //called in the pop-up window Form2: Private voidForm2_formclosing (Objectsender, FormClosingEventArgs e) { if( This. Owner isForm1) { ( This. Owner asFORM1). _remove ( This); } }
WinForm methods for popping unique forms