WinForm methods for popping unique forms

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.