1,首先创建一个用户登录信息类LoginInfo,并创建四个封装字段,用来保存用户信息
2,创建登录窗体frmLogin,并编写代码,验证与用户是否登录成功
示例代码:
if (txtUser.Text.Trim() == "" || txtPwd.Text.Trim() == "")
{
MessageBox.Show("用户名或密码不能为空!", "提示");
}
else
{
string userName = txtUser.Text;
string pwd = txtPwd.Text;
bool isOk = false;
foreach (LoginInfo item in array)
{
if (item != null)
{
if (item.Email == userName && item.Password == pwd)
{
isOk = true;
frmMain frm = new frmMain();
frm.txtHelloUser.Text = "欢迎," + item.Name;
frm.Show();
this.Hide();
break;
}
}
if (isOk == false)
{
txtPwd.Text = "";
txtPwd.Focus();
}
}
}
3,创建主窗体frmMain,用户登录成功后直接跳转到主窗体,并显示欢迎+用户名
4,创建注册窗体frmRegist,并检查信息是否合格,如果合格保存到用户信息类中
示例代码:
LoginInfo array = new LoginInfo();
array.Name = txtName.Text;
array.Id = txtId.Text;
if (txtEmail.Text == txtCheckEmail.Text)
{
array.Email = txtEmail.Text;
}
else
{
array.Email = "";
}
if (txtPwd.Text == txtCheckPwd.Text)
{
array.Password = txtPwd.Text;
}
else
{
array.Password = "";
}
if (array.Email == "" || array.Password == "")
{
MessageBox.Show("密码或邮箱两次输入不一致!", "提示");
txtPwd.Text = "";
txtEmail.Text = "";
txtEmail.Focus();
}
else
{
MessageBox.Show("恭喜,通过验证!","提示");
for (int i = 0; i < fl.array.Length; i++)
{
if (fl.array[i] == null)
{
fl.array[i] = array;
break;
}
}
fl.Visible = true;
this.Close();
}