📄 login.cs
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Data;
namespace MKIms3
{
/// <summary>
/// Login 的摘要说明。
/// </summary>
public class Login : System.Windows.Forms.Form
{
private string u_name;
private string u_pass;
public delegate void SendMessLogin () ;
//定义委托类型
public event SendMessLogin SendLogin ;
private System.Windows.Forms.PictureBox pictureBox1;
private System.Windows.Forms.Label label_U;
private System.Windows.Forms.Label label_p;
private System.Windows.Forms.TextBox txt_uname;
private System.Windows.Forms.TextBox txt_upass;
private System.Windows.Forms.Button btn_ok;
private System.Windows.Forms.Button btn_esc;
private System.Windows.Forms.ErrorProvider errP_user;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public Login()
{
//
// 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(Login));
this.pictureBox1 = new System.Windows.Forms.PictureBox();
this.label_U = new System.Windows.Forms.Label();
this.label_p = new System.Windows.Forms.Label();
this.txt_uname = new System.Windows.Forms.TextBox();
this.txt_upass = new System.Windows.Forms.TextBox();
this.btn_ok = new System.Windows.Forms.Button();
this.btn_esc = new System.Windows.Forms.Button();
this.errP_user = new System.Windows.Forms.ErrorProvider();
this.SuspendLayout();
//
// pictureBox1
//
this.pictureBox1.Dock = System.Windows.Forms.DockStyle.Top;
this.pictureBox1.Image = ((System.Drawing.Image)(resources.GetObject("pictureBox1.Image")));
this.pictureBox1.Location = new System.Drawing.Point(0, 0);
this.pictureBox1.Name = "pictureBox1";
this.pictureBox1.Size = new System.Drawing.Size(386, 112);
this.pictureBox1.TabIndex = 0;
this.pictureBox1.TabStop = false;
//
// label_U
//
this.label_U.Location = new System.Drawing.Point(32, 121);
this.label_U.Name = "label_U";
this.label_U.Size = new System.Drawing.Size(56, 23);
this.label_U.TabIndex = 1;
this.label_U.Text = "用户名:";
this.label_U.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
//
// label_p
//
this.label_p.Location = new System.Drawing.Point(200, 121);
this.label_p.Name = "label_p";
this.label_p.Size = new System.Drawing.Size(60, 23);
this.label_p.TabIndex = 2;
this.label_p.Text = "密 码:";
this.label_p.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
//
// txt_uname
//
this.txt_uname.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.txt_uname.Location = new System.Drawing.Point(83, 120);
this.txt_uname.MaxLength = 10;
this.txt_uname.Name = "txt_uname";
this.txt_uname.TabIndex = 0;
this.txt_uname.Text = "";
//
// txt_upass
//
this.txt_upass.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.txt_upass.Location = new System.Drawing.Point(256, 120);
this.txt_upass.MaxLength = 10;
this.txt_upass.Name = "txt_upass";
this.txt_upass.PasswordChar = '*';
this.txt_upass.TabIndex = 1;
this.txt_upass.Text = "";
//
// btn_ok
//
this.btn_ok.BackColor = System.Drawing.SystemColors.ActiveBorder;
this.btn_ok.DialogResult = System.Windows.Forms.DialogResult.OK;
this.btn_ok.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.btn_ok.Location = new System.Drawing.Point(168, 152);
this.btn_ok.Name = "btn_ok";
this.btn_ok.Size = new System.Drawing.Size(72, 24);
this.btn_ok.TabIndex = 2;
this.btn_ok.Text = "登陆系统";
this.btn_ok.Click += new System.EventHandler(this.btn_ok_Click);
//
// btn_esc
//
this.btn_esc.DialogResult = System.Windows.Forms.DialogResult.Cancel;
this.btn_esc.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.btn_esc.Location = new System.Drawing.Point(272, 151);
this.btn_esc.Name = "btn_esc";
this.btn_esc.Size = new System.Drawing.Size(72, 24);
this.btn_esc.TabIndex = 3;
this.btn_esc.Text = "退出登陆";
this.btn_esc.Click += new System.EventHandler(this.btn_esc_Click);
//
// errP_user
//
this.errP_user.ContainerControl = this;
//
// Login
//
this.AcceptButton = this.btn_ok;
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.BackColor = System.Drawing.SystemColors.ActiveBorder;
this.CancelButton = this.btn_esc;
this.ClientSize = new System.Drawing.Size(386, 186);
this.Controls.Add(this.btn_esc);
this.Controls.Add(this.btn_ok);
this.Controls.Add(this.txt_upass);
this.Controls.Add(this.txt_uname);
this.Controls.Add(this.label_p);
this.Controls.Add(this.label_U);
this.Controls.Add(this.pictureBox1);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow;
this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
this.Name = "Login";
this.ShowInTaskbar = false;
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "系统登陆";
this.TopMost = true;
this.Closing += new System.ComponentModel.CancelEventHandler(this.Login_Closing);
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 单击验证用户的身份
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btn_ok_Click(object sender, System.EventArgs e)
{
Splasher.Status="正在检测用户名和密码......";
if(loing_first()==true)
{
this.Visible = false;
SendLogin();
}
}
private bool LoginCheck()
{
this.u_name = this.txt_uname.Text.Trim();
this.u_pass = this.txt_upass.Text.Trim();
DataBase myDB = new DataBase();
SqlDataReader myReader = null;
SqlParameter[] myParamers={new SqlParameter("@u_name",SqlDbType.Char,10)} ;
myParamers[0].Value = this.u_name;
myReader = myDB.RunProcedure("up_get_upass",myParamers);
if(myReader!=null)
{
if(myReader.Read())
{
if(myReader["u_pass"].ToString().Trim()==this.u_pass)
{
Splasher.Status = "用户验证通过,开始加载系统数据...";
App.u_name = myReader["u_name"].ToString();
if(myReader["u_flag"]==null)
{
App.u_flag = (int)flag.user;
}
else
{
App.u_flag = Convert.ToInt32(myReader["u_flag"]);
}
myReader.Close();
return true;
}
else
{
MessageBox.Show("对不起,密码错误!","错误",MessageBoxButtons.OK,MessageBoxIcon.Warning);
myReader.Close();
return false;
}
}
else
{
MessageBox.Show("该用户不存在,请检查您的输入是否有误!","错误",MessageBoxButtons.OK,MessageBoxIcon.Warning);
myReader.Close();
return false;
}
}
else
{
return false;
}
}
/// <summary>
/// 开始登陆验证前的数据检查工作
/// </summary>
private bool loing_first()
{
if(u_name_isnull() || u_pass_isnull())
{
MessageBox.Show("对不起用户名和密码不能为空!","错误",MessageBoxButtons.OK,MessageBoxIcon.Warning);
return false;
}
else
{
return LoginCheck();
}
}
/// <summary>
/// 检测用户名是否为空
/// </summary>
/// <returns></returns>
private bool u_name_isnull()
{
return (this.txt_uname.Text.Trim().Length==0);
}
/// <summary>
/// 检测用户密码是否为空
/// </summary>
/// <returns></returns>
private bool u_pass_isnull()
{
return (this.txt_upass.Text.Trim().Length==0);
}
/// <summary>
/// 取消登陆,退出程序
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btn_esc_Click(object sender, System.EventArgs e)
{
this.Close();
Application.Exit();
}
private void Login_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
Application.Exit();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -