Multiple forms:
First of all, the question to be thought of is:
1. Which is the main form
Problem: The main form is hidden, after closing other forms, the main form is not displayed/closed, then the program is closed
Method: Use the constructor to pass a value to the form to upload it to another form.
Form1:
usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Windows.Forms;namespacemultiple Forms { Public Partial classForm1:form { PublicForm1 () {InitializeComponent (); } Private voidButton1_Click (Objectsender, EventArgs e) { //Form1 is the login form, Form2 the main form,//How to open Form2 with the Form1 button, then let Form1 hide Form2 displayForm2 F2 =NewForm2 ( This); F2. Show (); This. Hide ();//This is the current form, which refers to Form1, and then hide hides } }}
Form2:
usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Windows.Forms;namespacemultiple Forms { Public Partial classform2:form {Form1 F1=NULL; PublicForm2 (Form1 F1) {InitializeComponent (); F1=F1; } Private voidForm2_load (Objectsender, EventArgs e) { } Private voidForm2_formclosing (Objectsender, FormClosingEventArgs e) {F1. Close (); } }}
2. The form can only open one
Creates a global generic collection in order to place all open forms
1, before the form opens, determine whether there is a name consistent form in the collection, if there is a description is open, do not open the
Issue: When the form is open and closed, it cannot be opened again
Workaround: Clears this form object record from the collection in Form1 when the form is closed
Form1:
usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Windows.Forms;namespacemultiple Forms { Public Partial classForm1:form {//to create a global generic collectionList<form> flist =NewList<form>(); PublicForm1 () {InitializeComponent (); } Private voidButton1_Click (Objectsender, EventArgs e) { BOOLhas =false; //Form1 is the login form, Form2 the main form,//How to open Form2 with the Form1 button, then let Form1 hide Form2 displayForm2 F2 =NewForm2 ( This); foreach(Form FinchFlist)//go through the collection first to see if there are any forms and Form2 form content { if(F.name = = F2. Name)//If there is a match{ has=true; } } if(HAS)//If has is true { } Else//if has is not true{flist.add (F2); F2. Show (); } }//Issue: Click the button to open a form, close the form and then click on the form will not open//method: When the Form2 is closed, the Form1 collection is emptied, and the methods in Form1 are called in Form2 Public voidDeleteform (Form f) {flist.remove (f);//It's written here to remove. } }}
Form2:
usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Windows.Forms;namespacemultiple Forms { Public Partial classform2:form {Form1 F1=NULL; PublicForm2 (Form1 F1) {InitializeComponent (); F1=F1; } Private voidForm2_load (Objectsender, EventArgs e) { } Private voidForm2_formclosing (Objectsender, FormClosingEventArgs e) { //prevent error, Judge if(F1! =NULL) {F1. Deleteform ( This); } } }}
Issue: When the form is already open, clicking the Open button again will not re-display the open form and focus into
Workaround: Locate the Open object and set the WindowState property to:
Locate the object that has the form open and use the focus method;
Form1:
Private voidButton1_Click (Objectsender, EventArgs e) { BOOLhas =false; //Form1 is the login form, Form2 the main form,//How to open Form2 with the Form1 button, then let Form1 hide Form2 displayForm2 F2 =NewForm2 ( This); foreach(Form FinchFlist)//go through the collection first to see if there are any forms and Form2 form content { if(F.name = = F2. Name)//If there is a match{ has=true; F.windowstate= Formwindowstate.normal;//first set the WindowState property to: NormalF.focus ();//Focus into } } if(HAS)//If has is true{F2. Close ();//release the newly created form } Else//if has is not true{flist.add (F2); F2. Show (); } }
3. Transfer values and controls between forms
Pass value: Constructor pass value
Form1:
usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Windows.Forms;namespacetransfer values and controls between forms { Public Partial classForm1:form { PublicForm1 () {InitializeComponent (); } Private voidButton1_Click (Objectsender, EventArgs e) { //determine if user name input is correct if(TextBox1.Text = ="Zhangsan"&& TextBox2.Text = ="1234") {Form2 F2=NewForm2 ( This, TextBox1.Text); F2. Show (); This. Hide (); } } }}
Form2:
usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Windows.Forms;namespacetransfer values and controls between forms { Public Partial classform2:form {Form1 F1=NULL;//Global Variables//constructor overload, call this constructor, pass over a Form1 object, and string Uname PublicForm2 (Form1 F1,stringUname) {InitializeComponent (); F1=F1; Label1. Text="Welcome Back! "+Uname; } }}
Control: The first step, find the Form object,
The second step is to change the control value of the form object
Note: To modify the access rights of objects in a form
Form1.designer:
Private System.Windows.Forms.TextBox textBox1; Private System.Windows.Forms.TextBox textBox2; Private System.Windows.Forms.Label Label1; Private System.Windows.Forms.Label Label2; Public System.Windows.Forms.Button button1;
Form2:
Private void button1_click (object sender, EventArgs e) { // Remove the value of TextBox1 TextBox1.Text; if NULL { // Form1.designer inside Button1 private to public, so F1 can point out button1 F1.button1.Text = textbox1.text; } }
4. The form of the prompt box type
ShowDialog ();
Action: The current form can be manipulated and other forms cannot be manipulated
Close the current form before you can manipulate other forms
Form1:
Private void button2_click (object sender, EventArgs e) { Form2 f2=new Form2 ( this,"111"); F2. ShowDialog (); }
WinForm Multiple forms