1,首先,打开Visual Studio 2015版本,新建Windows窗体应用程序
结果:
2,点击右侧工具箱,如果右侧没有,则在视图里面找到工具箱点击,拖拽三个label,两个textbox,两个button控件
一些属性值大家可以自行设置,我在这只讲几个我认为不好找的一些属性值
如果要添加背景图片,则需要把label,button背景改为透明,如果不改则会像下图一样
改的方法:label backcolor属性中web下点击Transparent,
效果如下:
下面开始写登录事件处理
双击登录按钮进入
private void button1_Click(object sender, EventArgs e)
{
}
这个函数,
下面是我写的代码
private void login_Click(object sender, EventArgs e)
{
string account = AccountBox.Text;
string password = PasswordBox.Text.ToString();
if (student.Checked)
{
Student student = new Student(account, password);
loginCheck check = new loginCheck(student);
check.checkStudent();//进行账号密码判断
if (check.UserError)
{
if (check.PasswordError)
{
this.Hide();
new studentForm().Show();
}
else
{
//errorprovider控件提示信息
this.errorProvider1.SetError(this.PasswordBox, "密码错误");
}
}
else
{
//errorprovider控件提示信息
this.errorProvider1.SetError(this.AccountBox, "不存在用户名");
}
}
else if (teacher.Checked)
{
Teacher teacher = new Teacher(account, password);
loginCheck check = new loginCheck(teacher);
check.checkTeacher();//进行账号密码判断
if (check.UserError)
{
if (check.PasswordError)
{
this.Hide();
new teachFrom().Show();
}
else
{
//errorprovider控件提示信息
this.errorProvider1.SetError(this.PasswordBox, "密码错误");
}
}
else
{
//errorprovider控件提示信息
this.errorProvider1.SetError(this.AccountBox, "不存在用户名");
}
}
}
中间有用到其他类里面的函数
代码如下:
using System;
using System.Data.SqlClient;
using System.Security.Cryptography;
using System.Windows.Forms;
namespace MyClass
{
/*
* 学生登录账号密码类
*/
public class Student
{
private String _ID;
private String _password;
public Student()
{
}
public Student(String ID, String password)
{
this._ID = ID;
this.Password = password;
}
public string ID
{
get{return _ID; }
set { _ID = value; }
}
public string Password
{
get{ return _password; }
set{ _password = value; }
}
}
/*
* 教师登录账号密码类
*
*/
public class Teacher
{
private String _ID;
private String _password;
public Teacher(String ID, String password)
{
this._ID = ID;
this.Password = password;
}
public string ID
{
get { return _ID; }
set { _ID = value; }
}
public string Password
{
get {return _password; }
set { _password = value;}
}
}
/*
* 登录判断类
*/
public class loginCheck
{
private Student student;
private Teacher teacher;
private Boolean userError = false;
private Boolean passwordError = false;
private SqlConnection conn;
private string pass;
public bool UserError
{
get{return userError; }
set { userError = value; }
}
public bool PasswordError
{
get { return passwordError; }
set { passwordError = value; }
}
//private
public loginCheck(Student student)
{
this.student = student;
student.Password = new passwordEncryption().MD5(student.Password);
}
public loginCheck(Teacher teacher)
{
this.teacher = teacher;
teacher.Password = new passwordEncryption().MD5(teacher.Password);
}
private void SQL()
{
conn = new SqlConnection("server=.;uid=sa;pwd=sq0318..;database=数据库综合实验");
pass = null;
conn.Open();
}
private void SQL_1(string sqlStr)
{
SqlCommand cmd = new SqlCommand(sqlStr, conn);
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
pass = dr["password"].ToString();
UserError = true;
}
if (UserError == true)
{
if (teacher.Password == pass)
{
PasswordError = true;
}
}
}
public void checkStudent()
{
SQL();
string sqlStr = "select * from student where Sno = '" + student.ID + "'";
SQL_1(sqlStr);
}
public void checkTeacher()
{
SQL();
string sqlStr = "select password from teacher where Tno = '" + teacher.ID + "'";
SQL_1(sqlStr);
}
}
/*
* 密码进行加密类
*/
public class passwordEncryption
{
/*
* 密码加密方法
*/
public String MD5(String s)
{
char[] hexDigits = { '0', '1', '2', '3', '4', '5', '6',
'7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
try
{
byte[] btInput = System.Text.Encoding.UTF8.GetBytes(s);
// 获得MD5摘要算法的 MessageDigest 对象
MD5 mdInst = System.Security.Cryptography.MD5.Create();
// 使用指定的字节更新摘要
mdInst.ComputeHash(btInput);
// 获得密文
byte[] md = mdInst.Hash;
// 把密文转换成十六进制的字符串形式
int j = md.Length;
char[] str = new char[j * 2];
int k = 0;
for (int i = 0; i < j; i++)
{
byte byte0 = md[i];
str[k++] = hexDigits[(int)(((byte)byte0) >> 4) & 0xf];
str[k++] = hexDigits[byte0 & 0xf];
}
return new string(str);
}
catch
{
// Console.Error.WriteLine(e.StackTrace);
return null;
}
}
}
}
以上截图是我写好之后又重新弄得一个工程进行截得图,下面是我自己的截图
这个登录界面源码在https://download.csdn.net/download/qq_42110740/10487238这可以下载
之后每实现一点就会发上去一点,