⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 logonform.cs

📁 windows mobile 基于XML存储数据的图书管理系统
💻 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 调用后添加任何构造函数代码
			//
		}

		/// <summary>
		/// 清理所有正在使用的资源。
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			base.Dispose( disposing );
		}

		#region Windows 窗体设计器生成的代码
		/// <summary>
		/// 设计器支持所需的方法 - 不要使用代码编辑器修改
		/// 此方法的内容。
		/// </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 + -