基于WinForms(C#)的用户登录界面(VisualStudio2013)
来源:互联网 发布:webpack 优化 编辑:程序博客网 时间:2024/05/21 15:41
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();
}
}
}
- 基于WinForms(C#)的用户登录界面(VisualStudio2013)
- 用户登录界面(代码)
- 使用session验证的用户登录界面(简写)
- 设计包含多种控件的用户登录界面(1)
- Bootstrap用户登录界面的实现(BootStrap入门2)
- 包含.cpp .h的文件怎么运行(VisualStudio2013)
- 基于JAVA的Ajax与登录界面尝试(一)
- Android-------模拟用户登录界面(1)
- Android-------模拟用户登录界面(2)
- Android-------模拟用户登录界面(3)
- Android-------模拟用户登录界面(4)
- Android-------模拟用户登录界面(5)
- 有趣的用户登录界面
- 用户登录界面的设置
- 用户登录界面的设置
- WinForms程序使用委托(Invoke)的方式操作界面的控件
- 【C++】登录界面(透明文字显示)
- 含文本框的提示框的使用->UIAlertController类的使用(用户登录界面实例)
- nrf52832 SPi方式无操作系统与SD卡通信
- HTTP状态码
- NFS文件系统
- git常用命令及问题解决
- mycat分片
- 基于WinForms(C#)的用户登录界面(VisualStudio2013)
- C语言基础(三)
- Java进阶(二)文件读操作
- PL-SQL将oracle的表结构导出为word表格
- 2017年12月11日工作总结
- oracle知识点
- mysql执行计划
- hdoj 2141 二分 + 优化
- java线程池(2)-线程池添加任务的过程(原理)