?? logonform.cs
字號:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;
using System.Reflection;
namespace book
{
/// <summary>
/// LogonForm 的摘要說明。
/// </summary>
public class LogonForm : System.Windows.Forms.Form
{
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.TextBox txtUsername;
private System.Windows.Forms.TextBox txtPassword;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Data.DataSet dsAdmins;
private System.Data.DataTable dtAdmins;
public string Username
{
get
{
return txtUsername.Text;
}
}
public LogonForm()
{
//
// Windows 窗體設計器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 調用后添加任何構造函數(shù)代碼
//
}
/// <summary>
/// 清理所有正在使用的資源。
/// </summary>
protected override void Dispose( bool disposing )
{
base.Dispose( disposing );
}
#region Windows 窗體設計器生成的代碼
/// <summary>
/// 設計器支持所需的方法 - 不要使用代碼編輯器修改
/// 此方法的內(nèi)容。
/// </summary>
private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.txtUsername = new System.Windows.Forms.TextBox();
this.txtPassword = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
//
// label1
//
this.label1.Location = new System.Drawing.Point(24, 16);
this.label1.Size = new System.Drawing.Size(48, 16);
this.label1.Text = "用戶名";
//
// label2
//
this.label2.Location = new System.Drawing.Point(24, 48);
this.label2.Size = new System.Drawing.Size(48, 24);
this.label2.Text = "密 碼";
//
// txtUsername
//
this.txtUsername.Location = new System.Drawing.Point(89, 16);
this.txtUsername.Size = new System.Drawing.Size(119, 21);
this.txtUsername.Text = "";
//
// txtPassword
//
this.txtPassword.Location = new System.Drawing.Point(89, 43);
this.txtPassword.PasswordChar = '*';
this.txtPassword.Size = new System.Drawing.Size(118, 21);
this.txtPassword.Text = "";
//
// button1
//
this.button1.Location = new System.Drawing.Point(24, 80);
this.button1.Size = new System.Drawing.Size(48, 24);
this.button1.Text = "登錄";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(136, 80);
this.button2.Size = new System.Drawing.Size(48, 24);
this.button2.Text = "取消";
this.button2.Click += new System.EventHandler(this.button2_Click);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
//
// LogonForm
//
this.BackColor = System.Drawing.Color.Silver;
this.ClientSize = new System.Drawing.Size(224, 120);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Controls.Add(this.txtPassword);
this.Controls.Add(this.txtUsername);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Text = "登錄";
this.Load += new System.EventHandler(this.LogonForm_Load);
}
#endregion
private bool CheckUser(string username)
{
return dtAdmins.Rows.Find(username) != null;
}
private bool CheckPassword(string username,string password)
{
DataRow row = dtAdmins.Rows.Find(username);
if(row == null) return false;
return row["password"].ToString() == password;
}
private void LogonForm_Load(object sender, System.EventArgs e)
{
string path = Assembly.GetExecutingAssembly().GetName().CodeBase;
dsAdmins = new DataSet();
dsAdmins.ReadXml(new FileInfo(path).DirectoryName + @"\" + "user.xml");
dtAdmins = dsAdmins.Tables[0];
DataColumn[] pkey = new DataColumn[1];
pkey[0] = dtAdmins.Columns["username"];
dtAdmins.PrimaryKey = pkey;
}
private void button1_Click(object sender, System.EventArgs e)
{
if (!CheckUser(txtUsername.Text))
{
MessageBox.Show("用戶名不存在,請重新輸入。","登錄錯誤");
}
else if (!CheckPassword(txtUsername.Text, txtPassword.Text))
{
MessageBox.Show("口令不正確,請重新輸入。","登錄錯誤");
}
else
this.DialogResult = DialogResult.OK;
}
private void button2_Click(object sender, System.EventArgs e)
{
this.DialogResult = DialogResult.Cancel;
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -