📄 frmlogin.cs
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using prjClass;
using System.Data.SqlClient;
namespace prjLogin
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class frmLogin : System.Windows.Forms.Form
{
public event LoginHandler LoginEvent;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.TextBox txtName;
private System.Windows.Forms.Button btnOK;
private System.Windows.Forms.Button btnCancle;
private System.Windows.Forms.TextBox txtPwd;
private System.Windows.Forms.Label label2;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public delegate void LoginHandler(string user,string pur);
private string pur;
private string user;
private bool loginFlag = false;
public frmLogin()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(frmLogin));
this.label1 = new System.Windows.Forms.Label();
this.txtName = new System.Windows.Forms.TextBox();
this.btnOK = new System.Windows.Forms.Button();
this.btnCancle = new System.Windows.Forms.Button();
this.txtPwd = new System.Windows.Forms.TextBox();
this.label2 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// label1
//
this.label1.Location = new System.Drawing.Point(64, 32);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(48, 16);
this.label1.TabIndex = 0;
this.label1.Text = "用户名";
//
// txtName
//
this.txtName.Location = new System.Drawing.Point(112, 32);
this.txtName.Name = "txtName";
this.txtName.Size = new System.Drawing.Size(152, 21);
this.txtName.TabIndex = 1;
this.txtName.Text = "";
//
// btnOK
//
this.btnOK.BackColor = System.Drawing.SystemColors.ControlText;
this.btnOK.ForeColor = System.Drawing.SystemColors.HighlightText;
this.btnOK.Location = new System.Drawing.Point(64, 144);
this.btnOK.Name = "btnOK";
this.btnOK.TabIndex = 3;
this.btnOK.Text = "确定";
this.btnOK.Click += new System.EventHandler(this.btnOK_Click);
//
// btnCancle
//
this.btnCancle.BackColor = System.Drawing.SystemColors.ControlText;
this.btnCancle.ForeColor = System.Drawing.SystemColors.HighlightText;
this.btnCancle.Location = new System.Drawing.Point(216, 144);
this.btnCancle.Name = "btnCancle";
this.btnCancle.TabIndex = 4;
this.btnCancle.Text = "取消";
this.btnCancle.Click += new System.EventHandler(this.btnCancle_Click);
//
// txtPwd
//
this.txtPwd.Location = new System.Drawing.Point(112, 95);
this.txtPwd.Name = "txtPwd";
this.txtPwd.PasswordChar = '*';
this.txtPwd.Size = new System.Drawing.Size(152, 21);
this.txtPwd.TabIndex = 2;
this.txtPwd.Text = "";
//
// label2
//
this.label2.Location = new System.Drawing.Point(64, 95);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(48, 16);
this.label2.TabIndex = 4;
this.label2.Text = "密码";
//
// frmLogin
//
this.AcceptButton = this.btnOK;
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.BackColor = System.Drawing.SystemColors.ControlText;
this.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("$this.BackgroundImage")));
this.ClientSize = new System.Drawing.Size(338, 191);
this.ControlBox = false;
this.Controls.Add(this.txtPwd);
this.Controls.Add(this.label2);
this.Controls.Add(this.btnCancle);
this.Controls.Add(this.btnOK);
this.Controls.Add(this.txtName);
this.Controls.Add(this.label1);
this.ForeColor = System.Drawing.SystemColors.ControlLightLight;
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
this.MaximizeBox = false;
this.Name = "frmLogin";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "请您登录";
this.Closing += new System.ComponentModel.CancelEventHandler(this.frmLogin_Closing);
this.Load += new System.EventHandler(this.frmLogin_Load);
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 检查用户名及密码是否正确
/// </summary>
/// <returns></returns>
private bool check()
{
bool flag = false;
DataAccess da = new DataAccess();
string Sql = "select * from Class..UserInfo where UserID = '"+this.txtName.Text+"' and UserPwd = '"+this.txtPwd.Text+"'";
try
{
SqlDataReader sr = da.ExecuteQuery(Sql);
while(sr.Read()==true)
{
if(sr.HasRows==true)
{
this.user=sr.GetValue(0).ToString();
this.pur=sr.GetValue(2).ToString();
flag=true;
}
}
sr.Close();
}
finally
{
da.CloseCnn();
}
if(flag==true)
{
return true;
}
else
{
return false;
}
}
private void btnOK_Click(object sender, System.EventArgs e)
{
if(check()==true)
{
if(this.LoginEvent!=null)
{
this.LoginEvent(this.user,this.pur);
}
this.loginFlag=true;
this.Close();
}
else
{
MessageBox.Show("输入有误","提示信息",MessageBoxButtons.OK,MessageBoxIcon.Question);
}
}
private void frmLogin_Load(object sender, System.EventArgs e)
{
}
private void btnCancle_Click(object sender, System.EventArgs e)
{
this.loginFlag=false;
this.Close();
}
private void frmLogin_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
if(this.loginFlag==false)
{
Application.Exit();
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -