1.界面设计
其中包含验证码、找回密码、注册账号、登陆系统等基本功能
用户登陆界面代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace GlobeSchool.登陆界面
{
public partial class LoginForm : Form
{
#region 数据库连接
string strcon = @"Data Source=DESKTOP-NHFOQPB;Initial catalog=InformationRegister;Integrated Security=True";
#endregion
public LoginForm()
{
InitializeComponent();
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
}
private void LoginForm_Load(object sender, EventArgs e)
{
txt_Name.Focus(); //光标跳到用户名框
btn_vercode_Click(sender, e);
}
private void cbo_LoginType_SelectedIndexChanged(object sender, EventArgs e)
{
}
#region 取消按钮+btn_Cancel
/// <summary>
/// 取消
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btn_Cancel_Click(object sender, EventArgs e)
{
this.Close();
}
#endregion
#region 登陆按钮+btn_Login
/// <summary>
/// 登陆button
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btn_Login_Click(object sender, EventArgs e)
{
if (txt_Name.Text == "" || txt_PassWord.Text == "") /*判断用户名或密码是否填写完整*/
{
MessageBox.Show("用户名或密码未填写,请填写完善!");
}
else
{
SqlConnection con = new SqlConnection(strcon);
try
{
string sql = "select * from UserLogin where UserLogin.UserName=@username and UserLogin.UserPassword=@pwd";
SqlCommand cmd = new SqlCommand(sql, con);
cmd.Parameters.AddWithValue("@username", txt_Name.Text);
cmd.Parameters.AddWithValue("@pwd", txt_PassWord.Text);
con.Open();
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
if ((dt.Rows.Count > 0) && (txt_Verification.Text == RANS.Text))
{
MessageBox.Show("欢迎使用本系统!");
MainForm aq = new MainForm();
aq.Show();
this.Hide();
}
else
{
if (!(dt.Rows.Count > 0))
{
MessageBox.Show("用户名或密码错误!请重新输入!");
txt_PassWord.Text = "";
txt_PassWord.Focus();
btn_vercode_Click(sender, e); //重新生成验证码
txt_Verification.Text = "";
}
if ((dt.Rows.Count > 0) && (txt_Verification.Text != RANS.Text))
{
MessageBox.Show("验证码输入错误!请重新输入!");
btn_vercode_Click(sender, e); //重新生成验证码
txt_Verification.Text = "";
txt_Verification.Focus();
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
con.Close();
}
}
}
#endregion
#region 回车键光标跳转至密码框
/// <summary>
/// /*通过回车键光标跳转至密码框*/
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void txt_Name_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == 13)
{
txt_PassWord.Focus();
}
}
#endregion
/*回车登录功能*/
private void txt_PassWord_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == 13)
{
txt_Verification.Focus();
}
}
//注册界面入口
private void btn_Register_Click(object sender, EventArgs e)
{
RegisterForm am = new RegisterForm();
am.Show();
this.Hide();
}
#region 产生随机验证码
private void btn_vercode_Click(object sender, EventArgs e)
{
Random ran = new Random();
RANS.Text = ran.Next(1000, 9999).ToString();
}
#endregion
private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
btn_vercode_Click(sender, e);
}
private void txt_Verification_TextChanged(object sender, EventArgs e)
{
}
private void txt_Verification_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == 13)
{
btn_Login_Click(sender,e);
}
}
private void txt_Name_TextChanged(object sender, EventArgs e)
{
}
private void btn_PasswordRecovery_Click(object sender, EventArgs e)
{
PassWordRecoveryForm mm = new PassWordRecoveryForm();
mm.Show();
this.Hide();
}
}
}
2.用户注册界面
代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace GlobeSchool.登陆界面
{
public partial class RegisterForm : Form
{
/* 数据库连接 */
string strcon = @"Data Source=DESKTOP-NHFOQPB;Initial catalog=InformationRegister;Integrated Security=True";
public RegisterForm()
{
InitializeComponent();
}
private void btn_OK_Click(object sender, EventArgs e)
{
/* 将信息添加到数据库 */
SqlConnection sqlcon = null;
if (txtName.Text == "" || txtEmail.Text == "" || txtPassWord.Text == "" || txtPhone.Text == "" || txtRePassWord.Text == "")
{
MessageBox.Show("(*)必填信息未填满!请填满!");
}
else
{
if (txtPassWord.Text == txtRePassWord.Text)
{
try
{
string stuID = txtNums.Text.Trim();
string stuName = txtName.Text.Trim();
string stuEmail = txtEmail.Text.Trim();
string stuPassWord = txtPassWord.Text.Trim();
string stuPhone = txtPhone.Text.Trim();
sqlcon = new SqlConnection(strcon);
SqlCommand command = new SqlCommand();
command.CommandText = "INSERT INTO UserLogin(UserName,UserPassword,Id,Phone,Email)VALUES('" + stuName + "','" + stuPassWord + "','" + stuID + "','" + stuPhone + "','" + stuEmail + "')";
command.CommandType = CommandType.Text;
command.Connection = sqlcon;
sqlcon.Open();
if (command.ExecuteNonQuery() == 1)
{
MessageBox.Show("注册成功!\n请重新登录!");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
sqlcon.Close();
}
LoginForm dd = new LoginForm();
dd.Show();
this.Close();
}
else
{
MessageBox.Show("密码错误,请重新输入密码!");
txtPassWord.Text = "";
txtRePassWord.Text = "";
txtPassWord.Focus();
}
}
}
private void RegisterForm_Load(object sender, EventArgs e)
{
txtName.Focus();
}
private void btn_Cancel_Click(object sender, EventArgs e)
{
LoginForm mm = new LoginForm();
mm.Show();
this.Close();
}
private void txtName_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == 13)
{
txtNums.Focus();
}
}
private void txtNums_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == 13)
{
txtPhone.Focus();
}
}
private void txtPhone_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == 13)
{
txtEmail.Focus();
}
}
private void txtEmail_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == 13)
{
txtPassWord.Focus();
}
}
private void txtPassWord_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == 13)
{
txtRePassWord.Focus();
}
}
private void txtRePassWord_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == 13)
{
btn_OK_Click(sender,e);
}
}
}
}
3.找回密码界面设计
代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace GlobeSchool.登陆界面
{
public partial class PassWordRecoveryForm : Form
{
/*数据库连接*/
string strcon = @"Data Source=DESKTOP-NHFOQPB;Initial catalog=InformationRegister;Integrated Security=True";
public PassWordRecoveryForm()
{
InitializeComponent();
}
private void PassWordRecoveryForm_Load(object sender, EventArgs e)
{
txt_username.Focus();
}
private void txt_username_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == 13)
{
txt_nums.Focus();
}
}
private void txt_nums_TextChanged(object sender, EventArgs e)
{
}
private void txt_nums_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == 13)
{
btn_search_Click(sender,e);
}
}
private void btn_search_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(strcon);
string sql = "select UserLogin.UserPassword from UserLogin where UserLogin.UserName=@username and UserLogin.Id=@nums";
SqlCommand cmd = new SqlCommand(sql, con);
cmd.Parameters.AddWithValue("@username", txt_username.Text);
cmd.Parameters.AddWithValue("@nums", txt_nums.Text);
con.Open();
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
if (dt.Rows.Count > 0)
{
//SqlCommand cmd1 = new SqlCommand("select 班级 from UserLogin where id=...");
//string banji = cmd.executescalar().toString();
password_appear.Text = "您的密码是:" + cmd.ExecuteScalar();
password_appear.Visible = true;
}
else
{
MessageBox.Show("信息不匹配,请重新输入!");
txt_username.Text = "";
txt_nums.Text = "";
txt_username.Focus();
}
//if()
//else
//{
//MessageBox.Show("信息不匹配,请重新输入!");
//txt_username.Text = "";
//txt_nums.Text = "";
//txt_username.Focus();
//}
}
private void btn_exit_Click(object sender, EventArgs e)
{
LoginForm ak = new LoginForm();
ak.Show();
this.Close();
}
}
}