c#-winform-cross-form constructor pass-through value and application-Login window value, how to close the application when the main page is closed, how to open a unique window-★★★★★ Five Star heavyweight

Source: Internet
Author: User

Constructors can pass values of any type and can pass multiple values at the same time

Initial application of the value of structure function--Simple landing interface

Now that I have two forms Form3 and Form4, how do you click the button in Form3, open Form4 and pass the value in the textbox in FORM3 to the FORM4 label?

1. Click the button in FORM3 to open FORM4 2, get the value in FORM3 3, pass the value to the label in FORM4

First, get the value of textbox1 in FORM3

Button1 Click events:

namespaceNotepad { Public Partial classForm3:form { PublicForm3 () {InitializeComponent (); }        //Button1 Click events        Private voidButton1_Click (Objectsender, EventArgs e) {            strings = TextBox1.Text;//get the value of a textbox in FORM3FORM4 f4=NewFORM4 ();//Create a new FORM4F4. Show ();//Show Form4        }    }
gets the value of button1 in Form3 and opens the Form4 form

Passing a value through a constructor- when a new FORM4 is created, the value is passed in, while the FORM4 's struct function receives the same type of value , such as the value passed in the string type in the example

namespaceNotepad { Public Partial classForm3:form { PublicForm3 () {InitializeComponent (); }        //Button1 Click events        Private voidButton1_Click (Objectsender, EventArgs e) {            strings = TextBox1.Text;//get the value of a textbox in FORM3FORM4 f4=NewFORM4 (s);//creates a new FORM4 and passes the data s into theF4. Show ();//Show Form4        }    }}
FORM3 Pass Value
namespace Notepad {    publicpartialclass  form4:form    {        string  null; // define a variable         Public FORM4 (string  txt)        {            InitializeComponent ();             = txt; // variable receives the value passed in         }    }}
FORM4 receive the value passed in

Assign the received value to Label1

namespace Notepad {    publicpartialclass  form4:form    {        string null; // define a variable         Public FORM4 (string  txt)        {            InitializeComponent ();             = txt; // variable receives the value            passed in Label1. Text = str; // Assign Value         }    }}
Assign Value

Four, test:

=============================================================

Initial application of the value of structure function--close application

How do I close a program? For example, the system starts with the < start Page Form3>, and when you open < home page form4>, closing FORM4 does not close the program, and the program closes when FORM3 is turned off.

However, when you close the < Main page form4> can not close < start Page FORM3>, the program still can't quit, at this time how to close the program?

Method: The < start page form3> form incoming < Main page Form4>, at this time FORM4 can control the FORM3 behavior, set FORM4 shutdown, FORM3 also shut down, you can implement application shutdown.

Pass the form in? When you pass a form in, you can implement control of another form in one form .

How do I pass a form in? (This-the form in which the code resides)

  Public Partial classForm3:form { PublicForm3 () {InitializeComponent (); }        //Button1 Click events        Private voidButton1_Click (Objectsender, EventArgs e) {FORM4 F4=NewFORM4 ( This);//creates a new FORM4 and passes the form itselfF4. Show ();//Show Form4        }    }
FORM3 Pass Value
 Public Partial class Form4:form    {        new Form3 (); // instantiate a FORM3 variable         Public FORM4 (Form3 F3)// incoming form         {            initializecomponent ();             = F3; // Receive Form         }    }
FROM4 receiving the pass value

FORM3 off when setting Form4 off

 Public Partial classform4:form {Form3 F3=NewFORM3 ();//instantiate a FORM3 variable         PublicFORM4 (Form3 F3)//incoming Form{InitializeComponent (); F3= F3;//Receive Form        }        //FORM4 when closed        Private voidForm4_formclosing (Objectsender, FormClosingEventArgs e) {F3. Close ();//Form3 form Close command        }    }
Form3 off when setting Form4 off

Test can

Similarly, how to hide Form3 when FORM4 is turned on?

New FORM3 (); // instantiate a FORM3 variable         Public FORM4 (Form3 F3)// incoming form         {            initializecomponent ();             = F3; // Receive form            false; // Form3 form does not display        }
Hide Login Interface

=============================================================

Preliminary application of the value of structure function--open unique form

In the example above, setting FORM4 at startup From3 not be hidden, and closing Form4 without turning off Form3, you will find that each time you click on the From3 button will appear a new FORM4

How do I implement open unique forms?

Method: Set a variable in FORM3 public int count= 0 (the access modifier must be public in order to be accessible across forms); if Count is 0, the new FORM4 is opened, then count + +, if count is not 0, Do not open the new Form4.

If FORM4 is turned off, the count in Form3 is still not 0, and the point button is not open Form4, which means that only one FORM4 is implemented.

How do I change the count in Form3 to 0 when I close FORM4? If count is passed to FORM4,FORM3, it is inaccessible to count in FORM4.

FORM3 is passed into FORM4, and count is changed to 0 when the FORM4 is closed.

 Public Partial classForm3:form { Public intCount =0;  PublicForm3 () {InitializeComponent (); }        //Button1 Click events        Private voidButton1_Click (Objectsender, EventArgs e) {            if(Count = =0) {Count++; FORM4 f4=NewFORM4 ( This);//creates a new FORM4 and passes the form itselfF4. Show ();//Show Form4            }        }    }
FORM3 Code
 Public Partial classform4:form {Form3 F3=NewFORM3 ();//instantiate a FORM3 variable         PublicFORM4 (Form3 F3)//incoming Form{InitializeComponent (); F3= F3;//Receive Form        }        //FORM4 when closed        Private voidForm4_formclosing (Objectsender, FormClosingEventArgs e) {F3.count--; }    }
FORM4 Code

Test to enable a unique window to be opened

c#-winform-cross-form constructor pass-through value and application-Login window value, how to close the application when the main page is closed, how to open a unique window-★★★★★ Five Star heavyweight

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.