基於WinForms(C#)的使用者登入介面(VisualStudio2013)
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
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 [email protected] and [email protected]";
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();
}
}
}