Winform main form Replace constructor pass value

Source: Internet
Author: User

To create a login form:

Make a login form, implement the Click button to close the form and open another form

Directly in the button click event, instantiate a form you want to open using the Show method and change the Visible property of the login form to False

New Form1 ();                F1. Show ();                  This false
View Code

Login interface

namespacewindowsformsapplication1{ Public Partial classLogin:form { Public stringUsername//define a variable to pass a value to the subform         PublicLogin () {InitializeComponent (); }        Private voidButton1_Click (Objectsender, EventArgs e) {            //Remove user name and password            stringUID =Txtuid.            Text; stringPWD =txtpwd.            Text; if(UID! =""&& pwd! ="") {Loginda da=NewLoginda ();//connecting to a database                if(DA. Denglu (UID, pwd))//Login Query                {                    //Assigning a user name to a member variable usernameUsername =uid; //change the return value of the form to OK                     This. DialogResult =DialogResult.OK; //Close the form                     This.                Close (); }                Else{MessageBox.Show ("The user name or password is wrong! "); }            }            Else{MessageBox.Show ("user name and password cannot be empty! "); }        }        Private voidLogin_load (Objectsender, EventArgs e) {        }    }}
View Code

Another: When you close the second window again, the program does not end completely, the first login window still exists, although it does not show

So we use the constructor to pass the value, and this can be implemented to open a unique window (do not open the same window repeatedly)

Constructor Pass Value:

Constructors can set parameters, and when you instantiate the form, you can pass in a parameter of the type that we need, such as clicking Login, passing a user custom object and the login form itself to the form that will be opened

        NULL ;          Public Form1 (Form2 F2, Users u)        {            InitializeComponent ();             == u.username; // example of passing in objects using         }
View Code

Because of the scope problem, the passed in parameters can only be used in the constructor, you can set a global variable outside, in the constructor to pass in the value passed in to the global variable, so that the other events in this program can be called arbitrarily, as above

Because Form1 is not the main form so closing Form1 does not end the program, we only need to write an event that is triggered when the Form1 is closed (formclosing or formclosed):

         Private void Form1_formclosing (object  sender,    FormClosingEventArgs e)        {            F2. Close ();        }
View Code

In this case, the F2 passed in above is used, and the Form2 is controlled by assigning a value to the global variable F2.

Open a unique form:

If you click on the same login button multiple times, open the form of multiple landing, it is unreasonable

The same can be controlled by the constructor pass value, creating a list<form> public collection in the main form

  Public Partial classForm1:form { PublicForm1 () {InitializeComponent (); }         PublicList<form> flist =NewList<form>(); Private voidButton1_Click (Objectsender, EventArgs e) {            intCount =0;//tag variable tag whether the form already exists in the collectionForm2 F2=NewForm2 ( This);//instantiate a Form2 form and pass the Form1 yourself in .            foreach(Form FinchFlist)//iterating through the collection            {                if(f isForm2) {Count++; }            }            if(Count = =0)//If there is no Form1 form in the collection, open it{flist.add (F2); F2.            Show (); }        }    }
View Code

Then, when the Form2 is closed, set the Shutdown event to delete the From2 itself from the collection

 Public Partial classform2:form {Form1 F1=NULL;//define a global variable for use below, first equals null, and is assigned when the constructor is run, that is, when the form is opened         PublicForm2 (Form1 F1) {InitializeComponent (); F1=F1; }        Private voidForm2_formclosing (Objectsender, FormClosingEventArgs e) {F1. Flist.remove ( This);//to remove Form2 himself from the collection.        }    }

In this way, when the Form2 in the open state, click the button will Form2 open, otherwise do not do anything, and when Form2 off, delete the Form2 in the collection, so click the button again when you can reopen Form2

After the ComboBox binds the data source, it can set the displayed data with its value data, the value data is not displayed, but can be used in the program, especially convenient to find the number of databases between multiple tables

 Private voidForm1_Load (Objectsender, EventArgs e) {UserData ud=NewUserData (); Combobox1.datasource= UD. Select1 ("Nation"); Combobox1.displaymember="name"; Combobox1.valuemember="Code"; UserData UD1=NewUserData (); Combobox2.datasource= Ud1. Select1 ("Class"); Combobox2.displaymember="name"; Combobox2.valuemember="Code"; }           //EnquiryCombobox1.selectedtext =u.nation; Combobox2.selectedtext=U.class; //Modify or create a newN.nation =comboBox1.SelectedValue.ToString (); N.class= ComboBox2.SelectedValue.ToString ();
View Code

Winform main form Replace constructor pass value

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.