Create a Windows form

Source: Internet
Author: User
Some people often ask how to create a login. Many people think that multithreading should be used. In fact, multithreading is not required at all. Next, I will write the entire process in detail to help beginners.

Assume that you have a project for the wondowsform application and the main form named form1 (that isProgramForm of the entry point)

1. Add a new form that will serve as your login form.

My example form is like this, and the text in textbox is its name.

2. Next in form2, that is, the login formCodeAdd to view

Private bool islogin = false;

Add it to the front of its constructor. We will use this item later.

Add an attribute

Public bool islogin
 
{

Get

{

Return this. islogin;

}
 
}

3. add your own verification code to the Click Event Processing Method of the login button, which can be read database or any other method. Here is a simple example.

If(this.txt userid. Text = "reeezak" & this.txt password. Text = "csdn ")

{

This. islogin = true;

This. Close (); // the login form is closed only when the login is successful.

}

Else

{

MessageBox. Show ("invalid user or password, try again! ");

}
 
4. In the main form, change the code

Form2 F2 = new form2 ();

F2.showdialog ();

If (f2.islogin = true)

{

Application. Run (New form1 ());

}

5. Finally, let's talk about the two buttons. You cannot set the dialogresult of the login button to "OK" (it is best to set it to none, Which is customized anyway). Otherwise, the login form will be closed no matter whether the login is successful or not. We already have this. Close () in its click event processing method, so we don't need it.

There are two methods for the cancel button. It is most convenient to set its dialogresult to cancel. If it is set to none, a click event processing method is added to it, and the content in it is a sentence, this. Close ();

Just a few codes in this way, the login form will be merged, is it very simple? Pai_^

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.