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_^