📄 formlogin.cs
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.Common;
using System.Data.SqlServerCe;
using System.Data.SqlTypes;
namespace WinCE_DBF
{
/// <summary>
/// FormLogIn 的摘要说明。
/// </summary>
public class FormLogIn : System.Windows.Forms.Form
{
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Button btLogin;
private System.Windows.Forms.Button btCancel;
private System.Windows.Forms.TextBox textBox2;
public string UName; //用户名
public FormLogIn()
{
//
// 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.textBox1 = new System.Windows.Forms.TextBox();
this.textBox2 = new System.Windows.Forms.TextBox();
this.btLogin = new System.Windows.Forms.Button();
this.btCancel = new System.Windows.Forms.Button();
//
// label1
//
this.label1.Font = new System.Drawing.Font("宋体", 12F, System.Drawing.FontStyle.Bold);
this.label1.ForeColor = System.Drawing.SystemColors.HotTrack;
this.label1.Location = new System.Drawing.Point(9, 12);
this.label1.Size = new System.Drawing.Size(80, 16);
this.label1.Text = "UserName:";
//
// label2
//
this.label2.Font = new System.Drawing.Font("宋体", 12F, System.Drawing.FontStyle.Bold);
this.label2.ForeColor = System.Drawing.SystemColors.HotTrack;
this.label2.Location = new System.Drawing.Point(9, 44);
this.label2.Size = new System.Drawing.Size(80, 20);
this.label2.Text = "PassWord:";
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(97, 11);
this.textBox1.MaxLength = 10;
this.textBox1.Size = new System.Drawing.Size(135, 21);
this.textBox1.Text = "";
this.textBox1.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.textBox1_KeyPress);
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(97, 44);
this.textBox2.MaxLength = 10;
this.textBox2.PasswordChar = '*';
this.textBox2.Size = new System.Drawing.Size(135, 21);
this.textBox2.Text = "";
this.textBox2.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.textBox2_KeyPress);
//
// btLogin
//
this.btLogin.Location = new System.Drawing.Point(32, 88);
this.btLogin.Text = "LogIn";
this.btLogin.Click += new System.EventHandler(this.btLogin_Click);
//
// btCancel
//
this.btCancel.Location = new System.Drawing.Point(136, 88);
this.btCancel.Text = "Cancel";
this.btCancel.Click += new System.EventHandler(this.btCancel_Click);
//
// FormLogIn
//
this.ClientSize = new System.Drawing.Size(242, 127);
this.Controls.Add(this.btCancel);
this.Controls.Add(this.btLogin);
this.Controls.Add(this.textBox2);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Text = "FormLogIn";
this.Load += new System.EventHandler(this.FormLogIn_Load);
}
#endregion
private void FormLogIn_Load(object sender, System.EventArgs e)
{
UName="";
this.Left=200;
this.Top=35;
this.textBox1.Focus();
}
private void LogIn()
{
//系统登录的实现部分
this.UName="";
if(this.textBox1.Text.Trim()=="")
{
MessageBox.Show("Please Input UserName!");
this.textBox1.Focus();
return;
}
if(this.textBox2.Text.Trim()=="")
{
MessageBox.Show("Please Input PassWord!");
this.textBox2.Focus();
return;
}
SqlCeConnection conn = null;
conn = new SqlCeConnection ("Data Source =SqlCeDB.sdf");
if(conn.State==ConnectionState.Closed)
conn.Open();
//conn.Open();
SqlCeCommand cmd = conn.CreateCommand();
cmd.CommandText = "SELECT * FROM pass";
//cmd.CommandText = "SELECT * FROM pass where USERR= ?";
//cmd.Parameters.Add(new SqlCeParameter("p1", SqlDbType.NText));
//cmd.Parameters["p1"].Size = 10;
//cmd.Parameters["p1"].Value=this.textBox1.Text.Trim();
SqlCeDataReader rdr = cmd.ExecuteReader();
while(rdr.Read())
{
if ((this.textBox1.Text.Trim()==rdr.GetString(0))&&(this.textBox2.Text.Trim()==rdr.GetString(1)))
{
this.UName=this.textBox1.Text.Trim();
rdr.Close();
break;
}
}
cmd.Parameters.Clear();
if (this.UName=="")
{
MessageBox.Show("UserName or PassWord Errror!");
conn.Close();
this.textBox1.Text="";
this.textBox2.Text="";
this.textBox1.Focus();
return;
}
else
this.DialogResult=DialogResult.OK;
if(conn.State==ConnectionState.Open)
conn.Close();
this.Close();
}
private void btLogin_Click(object sender, System.EventArgs e)
{
//系统登录
LogIn();
}
private void btCancel_Click(object sender, System.EventArgs e)
{
//取消登录
this.DialogResult=DialogResult.Cancel;
this.Close();
}
private void textBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
if(e.KeyChar==13)
LogIn();
}
private void textBox2_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
if(e.KeyChar==13)
LogIn();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -