WinForm登录模块设计开发
用户登录类:
public partial class UserLogin : DevComponents.DotNetBar.Office2007RibbonForm
{
private SceneViewer sceneViewer = new SceneViewer();
private UserManage userManage = new UserManage();
public UserLogin()
{
InitializeComponent();
}
private void btnCancel_Click(object sender, EventArgs e)
{
this.Close();
//MessageBox.Show("请输入正确的用户名和密码!", "登陆失败!", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
private void btnLogin_Click(object sender, EventArgs e)
{
if (this.editUserName.Text == string.Empty)
{
MessageBox.Show("用户名不能为空!");
this.editUserName.Focus();
return;
}
else if(userManage.UserDictionary.ContainsKey(this.editUserName.Text))
{
string psd = userManage.UserDictionary[this.editUserName.Text];
if (this.editPassword.Text == psd)
{
MessageBox.Show("欢迎登录本系统!", "登陆成功!", MessageBoxButtons.OK, MessageBoxIcon.Information);
this.DialogResult = DialogResult.OK;
}
else
{
MessageBox.Show("密码错误,请重新输入!", "密码错误!", MessageBoxButtons.OK, MessageBoxIcon.Error);
this.editPassword.Text = string.Empty;
this.editPassword.Focus();
}
}
else
{
MessageBox.Show("用户名不存在!","登录失败!",MessageBoxButtons.OK,MessageBoxIcon.Warning);
this.editUserName.Text = string.Empty;
this.editPassword.Text = string.Empty;
this.editUserName.Focus();
}
}
private void btnLogin_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
this.btnLogin.Click+=new EventHandler(btnLogin_Click);
}
}
private void UserLogin_Load(object sender, EventArgs e)
{
if (this.editUserName.Text == string.Empty && this.editPassword.Text == string.Empty)
{
this.btnLogin.Enabled = false;
}
}
private void editPassword_TextChanged(object sender, EventArgs e)
{
if (this.editUserName.Text == string.Empty && this.editPassword.Text == string.Empty)
this.btnLogin.Enabled = false;
else if (this.editUserName.Text == string.Empty && this.editPassword.Text != string.Empty)
this.btnLogin.Enabled = false;
else if (this.editUserName.Text != string.Empty && this.editPassword.Text == string.Empty)
this.btnLogin.Enabled = false;
else
this.btnLogin.Enabled = true;
}
}
主程序类:
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
UserLogin userLogin = new UserLogin();
try
{
if (userLogin.ShowDialog() == DialogResult.OK)
Application.Run(new SceneViewer());
else
Application.Exit();
}
finally
{
userLogin.Close();
}
}
}
至于用户的管理,若用户数量很少,且内部使用可以用Dictionary<key,value>进行管理;
若用户数很多的话,就用数据库的方式进行管理