经常在网上看到有人在问.NET的Login机制,很多人都是一个LoginForm一个MainForm,Login完了就把LoginForm隐藏,再把MainForm显示出来,个人觉得这个方法太过于表面,而事实上,.NET提供的ApplicationContext可以让我们写出更加优雅的Login机制,下面就演示给大家看看
首先,当然是先准备好一个MainForm和一个LoginForm啦
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

9
namespace DotNetLogin10


{11
public partial class LoginForm : Form12

{13
public event EventHandler<LoginEventArgs> OnLogin;14

15
public LoginForm()16

{17
InitializeComponent();18
}19

20
public void HandleLoginFailed()21

{22
MessageBox.Show("Login Failed!", "Login", MessageBoxButtons.OK, MessageBoxIcon.Information);23
ToggleUI(true);24
}25

26
private void btn_Login_Click(object sender, EventArgs e)27

{28
ToggleUI(false);29
string id = tbx_ID.Text;30
string password = tbx_Password.Text;31

32
if (!Validate(id, password))33

{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

42
private void btn_Cancel_Click(object sender, EventArgs e)43

{44
Close();45
}46

47
private bool Validate(string id, string pwd)48

{49
if (id != null && id.Trim() != "" && pwd != null && pwd.Trim() != "") return true;50
return false;51
}52

53
private void ToggleUI(bool enabled)54

{55
tbx_ID.Enabled = enabled;56
tbx_Password.Enabled = enabled;57
btn_Cancel.Enabled = enabled;58
btn_Login.Enabled = enabled;59
}60
}61

62
public class LoginEventArgs : EventArgs63

{64
protected string loginID;65
protected string password;66

67
public LoginEventArgs(string loginID, string password)68

{69
this.loginID = loginID;70
this.password = password;71
}72

73

public string LoginID
{ get
{ return loginID; } }74

75

public string Password
{ get
{ return password; } }76
}77
}78

1
namespace DotNetLogin2


{3
partial class LoginForm4

{5

/**//// <summary>6
/// Required designer variable.7
/// </summary>8
private System.ComponentModel.IContainer components = null;9

10

/**//// <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

{16
if (disposing && (components != null))17

{18
components.Dispose();19
}20
base.Dispose(disposing);21
}22

23

Windows Form Designer generated code#region Windows Form Designer generated code24

25

/**//// <summary>26
/// Required method for Designer support - do not modify27
/// the contents of this method with the code editor.28
/// </summary>29
private void InitializeComponent()30

{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
// label140
// 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
// label249
// 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_ID58
// 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_Password65
// 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_Login73
// 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_Cancel83
// 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
// LoginForm94
// 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

112
}113

114
#endregion115

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按钮,如下图

窗体准备完毕后,关键部分的ApplicationContext的代码随后奉上
1
using System;2
using System.Collections.Generic;3
using System.Text;4
using System.Windows.Forms;5

6
namespace DotNetLogin7


{8
public class LoginContext : ApplicationContext9

{10
protected LoginForm loginForm;11

12
public LoginContext()13

{14
loginForm = new LoginForm();15
MainForm = loginForm;16

17
loginForm.OnLogin += new EventHandler<LoginEventArgs>(loginForm_OnLogin);18
loginForm.Show();19
}20

21
private void loginForm_OnLogin(object sender, LoginEventArgs e)22

{23
if (Authenticate(e.LoginID, e.Password))24

{25
loginForm.Visible = false;26
MainForm main = new MainForm();27
MainForm = main;28
loginForm.Close();29
main.Show();30
}31
else32

{33
loginForm.HandleLoginFailed();34
}35
}36

37
private bool Authenticate(string loginID, string password)38

{39
//Just a test authentication40
if (loginID == "leowong" && password == "1234") return true;41
return false;42
}43
}44
}45

之后,主程序入口函数Main也得改改,因为这个时候就不能直接让它Application.Run(new MainForm());这样子了,这时候运行起来的应该是上面的LoginContext。最后的Program.cs文件改成下面的样子
1
using System;2
using System.Collections.Generic;3
using System.Windows.Forms;4

5
namespace DotNetLogin6


{7
static class Program8

{9

/**//// <summary>10
/// The main entry point for the application.11
/// </summary>12
[STAThread]13
static void Main()14

{15
Application.EnableVisualStyles();16
Application.SetCompatibleTextRenderingDefault(false);17
Application.Run(new LoginContext());18
}19
}20
}21

最后运行看看结果吧,希望对各位有帮助
浙公网安备 33010602011771号