经常在网上看到有人在问.NET的Login机制,很多人都是一个LoginForm一个MainForm,Login完了就把LoginForm隐藏,再把MainForm显示出来,个人觉得这个方法太过于表面,而事实上,.NET提供的ApplicationContext可以让我们写出更加优雅的Login机制,下面就演示给大家看看
首先,当然是先准备好一个MainForm和一个LoginForm啦
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
LoginForm.cs
1
using System;
2
using System.Collections.Generic;
3
using System.ComponentModel;
4
using System.Data;
5
using System.Drawing;
6
using System.Text;
7
using System.Windows.Forms;
8![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
9
namespace DotNetLogin
10![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
11
public partial class LoginForm : Form
12![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
13
public event EventHandler<LoginEventArgs> OnLogin;
14![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
15
public LoginForm()
16![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
17
InitializeComponent();
18
}
19![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
20
public void HandleLoginFailed()
21![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
22
MessageBox.Show("Login Failed!", "Login", MessageBoxButtons.OK, MessageBoxIcon.Information);
23
ToggleUI(true);
24
}
25![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
26
private void btn_Login_Click(object sender, EventArgs e)
27![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
28
ToggleUI(false);
29
string id = tbx_ID.Text;
30
string password = tbx_Password.Text;
31![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
32
if (!Validate(id, password))
33![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
34
MessageBox.Show("Invalid LoginID or Password!", "Login", MessageBoxButtons.OK, MessageBoxIcon.Information);
35
ToggleUI(true);
36
return;
37
}
38
if (OnLogin != null)
39
OnLogin(this, new LoginEventArgs(id, password));
40
}
41![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
42
private void btn_Cancel_Click(object sender, EventArgs e)
43![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
44
Close();
45
}
46![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
47
private bool Validate(string id, string pwd)
48![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
49
if (id != null && id.Trim() != "" && pwd != null && pwd.Trim() != "") return true;
50
return false;
51
}
52![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
53
private void ToggleUI(bool enabled)
54![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
55
tbx_ID.Enabled = enabled;
56
tbx_Password.Enabled = enabled;
57
btn_Cancel.Enabled = enabled;
58
btn_Login.Enabled = enabled;
59
}
60
}
61![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
62
public class LoginEventArgs : EventArgs
63![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
64
protected string loginID;
65
protected string password;
66![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
67
public LoginEventArgs(string loginID, string password)
68![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
69
this.loginID = loginID;
70
this.password = password;
71
}
72![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
73![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public string LoginID
{ get
{ return loginID; } }
74![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
75![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public string Password
{ get
{ return password; } }
76
}
77
}
78![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
LoginForm.Designer.cs
1
namespace DotNetLogin
2![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
3
partial class LoginForm
4![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
5![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
6
/// Required designer variable.
7
/// </summary>
8
private System.ComponentModel.IContainer components = null;
9![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
10![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
11
/// Clean up any resources being used.
12
/// </summary>
13
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
14
protected override void Dispose(bool disposing)
15![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
16
if (disposing && (components != null))
17![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
18
components.Dispose();
19
}
20
base.Dispose(disposing);
21
}
22![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
23![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
Windows Form Designer generated code#region Windows Form Designer generated code
24![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
25![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
26
/// Required method for Designer support - do not modify
27
/// the contents of this method with the code editor.
28
/// </summary>
29
private void InitializeComponent()
30![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
31
this.label1 = new System.Windows.Forms.Label();
32
this.label2 = new System.Windows.Forms.Label();
33
this.tbx_ID = new System.Windows.Forms.TextBox();
34
this.tbx_Password = new System.Windows.Forms.TextBox();
35
this.btn_Login = new System.Windows.Forms.Button();
36
this.btn_Cancel = new System.Windows.Forms.Button();
37
this.SuspendLayout();
38
//
39
// label1
40
//
41
this.label1.AutoSize = true;
42
this.label1.Location = new System.Drawing.Point(18, 30);
43
this.label1.Name = "label1";
44
this.label1.Size = new System.Drawing.Size(65, 12);
45
this.label1.TabIndex = 0;
46
this.label1.Text = "Login ID: ";
47
//
48
// label2
49
//
50
this.label2.AutoSize = true;
51
this.label2.Location = new System.Drawing.Point(18, 58);
52
this.label2.Name = "label2";
53
this.label2.Size = new System.Drawing.Size(65, 12);
54
this.label2.TabIndex = 1;
55
this.label2.Text = "Password: ";
56
//
57
// tbx_ID
58
//
59
this.tbx_ID.Location = new System.Drawing.Point(89, 27);
60
this.tbx_ID.Name = "tbx_ID";
61
this.tbx_ID.Size = new System.Drawing.Size(150, 21);
62
this.tbx_ID.TabIndex = 2;
63
//
64
// tbx_Password
65
//
66
this.tbx_Password.Location = new System.Drawing.Point(89, 55);
67
this.tbx_Password.Name = "tbx_Password";
68
this.tbx_Password.PasswordChar = '*';
69
this.tbx_Password.Size = new System.Drawing.Size(150, 21);
70
this.tbx_Password.TabIndex = 3;
71
//
72
// btn_Login
73
//
74
this.btn_Login.Location = new System.Drawing.Point(111, 82);
75
this.btn_Login.Name = "btn_Login";
76
this.btn_Login.Size = new System.Drawing.Size(61, 23);
77
this.btn_Login.TabIndex = 4;
78
this.btn_Login.Text = "Login";
79
this.btn_Login.UseVisualStyleBackColor = true;
80
this.btn_Login.Click += new System.EventHandler(this.btn_Login_Click);
81
//
82
// btn_Cancel
83
//
84
this.btn_Cancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
85
this.btn_Cancel.Location = new System.Drawing.Point(178, 82);
86
this.btn_Cancel.Name = "btn_Cancel";
87
this.btn_Cancel.Size = new System.Drawing.Size(61, 23);
88
this.btn_Cancel.TabIndex = 5;
89
this.btn_Cancel.Text = "Cancel";
90
this.btn_Cancel.UseVisualStyleBackColor = true;
91
this.btn_Cancel.Click += new System.EventHandler(this.btn_Cancel_Click);
92
//
93
// LoginForm
94
//
95
this.AcceptButton = this.btn_Login;
96
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
97
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
98
this.CancelButton = this.btn_Cancel;
99
this.ClientSize = new System.Drawing.Size(273, 120);
100
this.Controls.Add(this.btn_Cancel);
101
this.Controls.Add(this.btn_Login);
102
this.Controls.Add(this.tbx_Password);
103
this.Controls.Add(this.tbx_ID);
104
this.Controls.Add(this.label2);
105
this.Controls.Add(this.label1);
106
this.Name = "LoginForm";
107
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
108
this.Text = "Login";
109
this.ResumeLayout(false);
110
this.PerformLayout();
111![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
112
}
113![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
114
#endregion
115![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
116
private System.Windows.Forms.Label label1;
117
private System.Windows.Forms.Label label2;
118
private System.Windows.Forms.TextBox tbx_ID;
119
private System.Windows.Forms.TextBox tbx_Password;
120
private System.Windows.Forms.Button btn_Login;
121
private System.Windows.Forms.Button btn_Cancel;
122
}
123
}
MainForm就随便一个窗体得了,反正也只是演示作用,你们爱把MainForm做成咋样是咋样。而LoginForm,传统来说,需要主界面可以填上LoginID以及Password,另外还需要一个Login按钮以及一个Cancel按钮,如下图
![](https://images.cnblogs.com/cnblogs_com/leowong/login.jpg)
窗体准备完毕后,关键部分的ApplicationContext的代码随后奉上
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
LoginContext.cs
1
using System;
2
using System.Collections.Generic;
3
using System.Text;
4
using System.Windows.Forms;
5![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
6
namespace DotNetLogin
7![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
8
public class LoginContext : ApplicationContext
9![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
10
protected LoginForm loginForm;
11![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
12
public LoginContext()
13![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
14
loginForm = new LoginForm();
15
MainForm = loginForm;
16![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
17
loginForm.OnLogin += new EventHandler<LoginEventArgs>(loginForm_OnLogin);
18
loginForm.Show();
19
}
20![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
21
private void loginForm_OnLogin(object sender, LoginEventArgs e)
22![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
23
if (Authenticate(e.LoginID, e.Password))
24![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
25
loginForm.Visible = false;
26
MainForm main = new MainForm();
27
MainForm = main;
28
loginForm.Close();
29
main.Show();
30
}
31
else
32![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
33
loginForm.HandleLoginFailed();
34
}
35
}
36![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
37
private bool Authenticate(string loginID, string password)
38![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
39
//Just a test authentication
40
if (loginID == "leowong" && password == "1234") return true;
41
return false;
42
}
43
}
44
}
45![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
之后,主程序入口函数Main也得改改,因为这个时候就不能直接让它Application.Run(new MainForm());这样子了,这时候运行起来的应该是上面的LoginContext。最后的Program.cs文件改成下面的样子
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Program.cs
1
using System;
2
using System.Collections.Generic;
3
using System.Windows.Forms;
4![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
5
namespace DotNetLogin
6![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
7
static class Program
8![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
9![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
10
/// The main entry point for the application.
11
/// </summary>
12
[STAThread]
13
static void Main()
14![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
15
Application.EnableVisualStyles();
16
Application.SetCompatibleTextRenderingDefault(false);
17
Application.Run(new LoginContext());
18
}
19
}
20
}
21![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
最后运行看看结果吧,希望对各位有帮助