Login implementation in WinForm
- 2020-05-07 20:18:16
- OfStack
In a 1-like C/S system, the login function is basically a prerequisite,
Three methods to implement C# Winform login function are summarized.
type 1: changes the application entry to the main window in the application Settings code (Program.cs)
It sounds a bit convoluted, that is, when the application is initialized, the first page to load is the main page after we log in.
1. Set Application.Run in Program.cs:
2. Add the login button event private void button1_Click(object sender, EventArgs e) code
3, add the main window formMain load time event code
Design logic:
First, the program load main window formMain, then formMain will new1 login login window and pop-up window, and then close itself. this.DialogResult = DialogResult.OK; And close the login box. After closing the main_Load event of formMain again determines whether DialogResult of the login box is OK, and if so, loads the main window.
type 2: controls whether or not Application.Run () is executed by login authentication.
The code is as follows:
The principle of this method is simpler to understand than that of the first one. When the application is initialized, the login window is first loaded and verified.
type 3: directly hides the login box (note: hide, this.Hide () instead of this.Close ()) after passing the login verification. It's easier to understand this way.
Three methods to implement C# Winform login function are summarized.
type 1: changes the application entry to the main window in the application Settings code (Program.cs)
It sounds a bit convoluted, that is, when the application is initialized, the first page to load is the main page after we log in.
1. Set Application.Run in Program.cs:
Application.Run(new FormMain());// Sets the window to load when the application runs
2. Add the login button event private void button1_Click(object sender, EventArgs e) code
private void button1_Click(object sender, EventArgs e)// The login
{
if (this.textBoxPassword.Text == "")
{
MessageBox.Show(" Please enter your password! ");
}
else if (this.textBoxUsername.Text=="123" && this.textBoxPassword.Text == "123")
{
this.DialogResult = DialogResult.OK;
this.Close();
}
else
{
MessageBox.Show("Username or Password Error");
}
}
3, add the main window formMain load time event code
private void main_Load(object sender, EventArgs e)
{
Form formLogin = new login();
formLogin.ShowDialog();
if (formLogin.DialogResult == DialogResult.OK)// If the login box returns DialogResult.OK
{
MessageBox.Show(" Normal login ");
}
else
{
this.Close();
}
}
Design logic:
First, the program load main window formMain, then formMain will new1 login login window and pop-up window, and then close itself. this.DialogResult = DialogResult.OK; And close the login box. After closing the main_Load event of formMain again determines whether DialogResult of the login box is OK, and if so, loads the main window.
type 2: controls whether or not Application.Run () is executed by login authentication.
The code is as follows:
Form formLogin = new login();
formLogin.ShowDialog();
if (formLogin.DialogResult == DialogResult.OK)
{
Application.Run(new formMain());
}
else
{
return;
}
The principle of this method is simpler to understand than that of the first one. When the application is initialized, the login window is first loaded and verified.
type 3: directly hides the login box (note: hide, this.Hide () instead of this.Close ()) after passing the login verification. It's easier to understand this way.