| 订阅 | 在线投稿
分享
 
 
 

C#下WinForm编程:登录窗体的设计

来源:互联网  宽屏版  评论
2006-06-13 19:25:51

我在csdn里搜索了很久,也没有找到符合我要求的login文档,我这次把自己的心得和自己做的成果拿出来和大家分享一下,希望对后来的人能有一些帮助。我初次做,可能代码写的不是很规范,思路也不是很清晰,但是它能达到我要的效果就行了^_^ 希望哪位兄弟帮忙完善一下我的代码。

我在数据库里有一个 users 的表,如下:

ID UserName UserPasswd

1 admin admin

2 user user

3 guest guest

我准备这样做,先判断输入的用户名是否和表里的UserName相同,如果相同,再比较相同UserName下的UserPasswd,如果这些都正确了,就可以进入系统了。

全部代码如下(我把我写的部分用黑体):

Form1的代码:

using System;

using System.Drawing;

using System.Collections;

using System.ComponentModel;

using System.Windows.Forms;

using System.Data;

using System.Data.SqlClient;

namespace login

{

/// <summary>

/// Form1 的摘要说明。

/// </summary>

public class Form1 : System.Windows.Forms.Form

{

private System.Windows.Forms.Label label1;

private System.Windows.Forms.Label label2;

private System.Windows.Forms.TextBox txtUser;

private System.Windows.Forms.TextBox txtPasswd;

private System.Windows.Forms.Button btnOK;

private System.Windows.Forms.Button btnCancel;

/// <summary>

/// 必需的设计器变量。

/// </summary>

private System.ComponentModel.Container components = null;

public Form1()

{

//

// 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()

{

this.label1 = new System.Windows.Forms.Label();

this.label2 = new System.Windows.Forms.Label();

this.txtUser = new System.Windows.Forms.TextBox();

this.txtPasswd = new System.Windows.Forms.TextBox();

this.btnOK = new System.Windows.Forms.Button();

this.btnCancel = new System.Windows.Forms.Button();

this.SuspendLayout();

//

// label1

//

this.label1.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));

this.label1.Location = new System.Drawing.Point(40, 24);

this.label1.Name = "label1";

this.label1.Size = new System.Drawing.Size(64, 23);

this.label1.TabIndex = 0;

this.label1.Text = "用户名:";

//

// label2

//

this.label2.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));

this.label2.Location = new System.Drawing.Point(40, 72);

this.label2.Name = "label2";

this.label2.Size = new System.Drawing.Size(56, 23);

this.label2.TabIndex = 1;

this.label2.Text = "密码:";

//

// txtUser

//

this.txtUser.Location = new System.Drawing.Point(152, 24);

this.txtUser.Name = "txtUser";

this.txtUser.TabIndex = 2;

this.txtUser.Text = "";

//

// txtPasswd

//

this.txtPasswd.Location = new System.Drawing.Point(152, 72);

this.txtPasswd.Name = "txtPasswd";

this.txtPasswd.PasswordChar = '*';

this.txtPasswd.TabIndex = 3;

this.txtPasswd.Text = "";

//

// btnOK

//

this.btnOK.Location = new System.Drawing.Point(40, 120);

this.btnOK.Name = "btnOK";

this.btnOK.Size = new System.Drawing.Size(72, 23);

this.btnOK.TabIndex = 4;

this.btnOK.Text = "OK";

this.btnOK.Click += new System.EventHandler(this.btnOK_Click);

//

// btnCancel

//

this.btnCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;

this.btnCancel.Location = new System.Drawing.Point(176, 120);

this.btnCancel.Name = "btnCancel";

this.btnCancel.TabIndex = 5;

this.btnCancel.Text = "Cancel";

this.btnCancel.Click += new System.EventHandler(this.btnCancel_Click);

//

// Form1

//

this.AcceptButton = this.btnOK;

this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);

this.CancelButton = this.btnCancel;

this.ClientSize = new System.Drawing.Size(298, 167);

this.Controls.Add(this.btnCancel);

this.Controls.Add(this.btnOK);

this.Controls.Add(this.txtPasswd);

this.Controls.Add(this.txtUser);

this.Controls.Add(this.label2);

this.Controls.Add(this.label1);

this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;

this.MaximizeBox = false;

this.MaximumSize = new System.Drawing.Size(304, 192);

this.MinimizeBox = false;

this.MinimumSize = new System.Drawing.Size(304, 192);

this.Name = "Form1";

this.ShowInTaskbar = false;

this.Text = "Login";

this.Load += new System.EventHandler(this.Form1_Load);

this.ResumeLayout(false);

}

#endregion

/// <summary>

/// 应用程序的主入口点。

/// </summary>

private void btnCancel_Click(object sender, System.EventArgs e)

{

Application.Exit();

}

private void Form1_Load(object sender, System.EventArgs e)

{

this.SetDesktopLocation(280,180);

}

private void btnOK_Click(object sender, System.EventArgs e)

{

Form2 theOwner = (Form2)this.Owner;

if(this.txtUser.Text == "")

{

MessageBox.Show("用户名不能为空!","错误");

}

else if(this.txtPasswd.Text == "")

{

MessageBox.Show("密码不能为空!","错误");

}

else

{

if(IsUser(this.txtUser.Text))

{

if(this.txtPasswd.Text == LoginUser(this.txtUser.Text))

{

this.DialogResult = DialogResult.OK;

theOwner.getUserName = this.txtUser.Text;

}

else

{

MessageBox.Show("用户名或密码错误!");

}

}

else

{

MessageBox.Show("用户名或密码错误!");

}

}

}

private string LoginUser(string User)

{

SqlConnection conn = new SqlConnection("XXXXXXXX");

SqlCommand cmd = new SqlCommand();

cmd.CommandType = CommandType.StoredProcedure;

cmd.CommandText = "Login";

cmd.Connection = conn;

conn.Open();

SqlParameter parName = new SqlParameter("@Name",SqlDbType.VarChar,50);

parName.Value = User;

cmd.Parameters.Add(parName);

cmd.ExecuteNonQuery();

conn.Close();

SqlDataAdapter da = new SqlDataAdapter();

da.SelectCommand = cmd;

DataSet ds = new DataSet();

da.Fill(ds);

return ds.Tables[0].Rows[0]["UserPasswd"].ToString();

}

private bool IsUser(string User)

{

SqlConnection conn = new SqlConnection("XXXXXXXX");

SqlCommand cmd = new SqlCommand();

cmd.CommandType = CommandType.StoredProcedure;

cmd.CommandText = "IsUser";

cmd.Connection = conn;

conn.Open();

SqlParameter parName = new SqlParameter("@UserName",SqlDbType.VarChar,50);

parName.Value = User;

cmd.Parameters.Add(parName);

cmd.ExecuteNonQuery();

conn.Close();

SqlDataAdapter da = new SqlDataAdapter();

da.SelectCommand = cmd;

DataSet ds = new DataSet();

da.Fill(ds);

int n;

n = ds.Tables[0].Rows.Count;

if(n > 0)

return true;

else

return false;

}

}

}

在Form2中我设计了一个label ,让它接受从Form1传过来的UserName,这样我们在以后的设计中好判断用户的权限的大小。

Form2的代码:

using System;

using System.Drawing;

using System.Collections;

using System.ComponentModel;

using System.Windows.Forms;

using System.Data;

using System.Data.SqlClient;

namespace login

{

/// <summary>

/// Form2 的摘要说明。

/// </summary>

public class Form2 : System.Windows.Forms.Form

{

private string UserName;

private System.Windows.Forms.Label label1;

/// <summary>

/// 必需的设计器变量。

/// </summary>

private System.ComponentModel.Container components = null;

public Form2()

{

//

// Windows 窗体设计器支持所必需的

//

InitializeComponent();

Form1 myForm = new Form1();

myForm.ShowDialog(this);

//

// 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()

{

this.label1 = new System.Windows.Forms.Label();

this.SuspendLayout();

//

// label1

//

this.label1.Location = new System.Drawing.Point(128, 72);

this.label1.Name = "label1";

this.label1.Size = new System.Drawing.Size(152, 40);

this.label1.TabIndex = 0;

//

// Form2

//

this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);

this.ClientSize = new System.Drawing.Size(520, 357);

this.Controls.Add(this.label1);

this.Name = "Form2";

this.Text = "Form2";

this.Load += new System.EventHandler(this.Form2_Load);

this.ResumeLayout(false);

}

#endregion

[STAThread]

static void Main()

{

Application.Run(new Form2());

}

private void Form2_Load(object sender, System.EventArgs e)

{

this.SetDesktopLocation(200,180);

this.label1.Text = UserName;

}

public string getUserName

{

get

{

return UserName;

}

set

{

UserName = value;

}

}

}

}

 
我在csdn里搜索了很久,也没有找到符合我要求的login文档,我这次把自己的心得和自己做的成果拿出来和大家分享一下,希望对后来的人能有一些帮助。我初次做,可能代码写的不是很规范,思路也不是很清晰,但是它能达到我要的效果就行了^_^ 希望哪位兄弟帮忙完善一下我的代码。 我在数据库里有一个 users 的表,如下: ID UserName UserPasswd 1 admin admin 2 user user 3 guest guest 我准备这样做,先判断输入的用户名是否和表里的UserName相同,如果相同,再比较相同UserName下的UserPasswd,如果这些都正确了,就可以进入系统了。 全部代码如下(我把我写的部分用黑体): Form1的代码: using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; using System.Data.SqlClient; namespace login { /// <summary> /// Form1 的摘要说明。 /// </summary> public class Form1 : System.Windows.Forms.Form { private System.Windows.Forms.Label label1; private System.Windows.Forms.Label label2; private System.Windows.Forms.TextBox txtUser; private System.Windows.Forms.TextBox txtPasswd; private System.Windows.Forms.Button btnOK; private System.Windows.Forms.Button btnCancel; /// <summary> /// 必需的设计器变量。 /// </summary> private System.ComponentModel.Container components = null; public Form1() { // // 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() { this.label1 = new System.Windows.Forms.Label(); this.label2 = new System.Windows.Forms.Label(); this.txtUser = new System.Windows.Forms.TextBox(); this.txtPasswd = new System.Windows.Forms.TextBox(); this.btnOK = new System.Windows.Forms.Button(); this.btnCancel = new System.Windows.Forms.Button(); this.SuspendLayout(); // // label1 // this.label1.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134))); this.label1.Location = new System.Drawing.Point(40, 24); this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(64, 23); this.label1.TabIndex = 0; this.label1.Text = "用户名:"; // // label2 // this.label2.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134))); this.label2.Location = new System.Drawing.Point(40, 72); this.label2.Name = "label2"; this.label2.Size = new System.Drawing.Size(56, 23); this.label2.TabIndex = 1; this.label2.Text = "密码:"; // // txtUser // this.txtUser.Location = new System.Drawing.Point(152, 24); this.txtUser.Name = "txtUser"; this.txtUser.TabIndex = 2; this.txtUser.Text = ""; // // txtPasswd // this.txtPasswd.Location = new System.Drawing.Point(152, 72); this.txtPasswd.Name = "txtPasswd"; this.txtPasswd.PasswordChar = '*'; this.txtPasswd.TabIndex = 3; this.txtPasswd.Text = ""; // // btnOK // this.btnOK.Location = new System.Drawing.Point(40, 120); this.btnOK.Name = "btnOK"; this.btnOK.Size = new System.Drawing.Size(72, 23); this.btnOK.TabIndex = 4; this.btnOK.Text = "OK"; this.btnOK.Click += new System.EventHandler(this.btnOK_Click); // // btnCancel // this.btnCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel; this.btnCancel.Location = new System.Drawing.Point(176, 120); this.btnCancel.Name = "btnCancel"; this.btnCancel.TabIndex = 5; this.btnCancel.Text = "Cancel"; this.btnCancel.Click += new System.EventHandler(this.btnCancel_Click); // // Form1 // this.AcceptButton = this.btnOK; this.AutoScaleBaseSize = new System.Drawing.Size(6, 14); this.CancelButton = this.btnCancel; this.ClientSize = new System.Drawing.Size(298, 167); this.Controls.Add(this.btnCancel); this.Controls.Add(this.btnOK); this.Controls.Add(this.txtPasswd); this.Controls.Add(this.txtUser); this.Controls.Add(this.label2); this.Controls.Add(this.label1); this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog; this.MaximizeBox = false; this.MaximumSize = new System.Drawing.Size(304, 192); this.MinimizeBox = false; this.MinimumSize = new System.Drawing.Size(304, 192); this.Name = "Form1"; this.ShowInTaskbar = false; this.Text = "Login"; this.Load += new System.EventHandler(this.Form1_Load); this.ResumeLayout(false); } #endregion /// <summary> /// 应用程序的主入口点。 /// </summary> private void btnCancel_Click(object sender, System.EventArgs e) { Application.Exit(); } private void Form1_Load(object sender, System.EventArgs e) { this.SetDesktopLocation(280,180); } private void btnOK_Click(object sender, System.EventArgs e) { Form2 theOwner = (Form2)this.Owner; if(this.txtUser.Text == "") { MessageBox.Show("用户名不能为空!","错误"); } else if(this.txtPasswd.Text == "") { MessageBox.Show("密码不能为空!","错误"); } else { if(IsUser(this.txtUser.Text)) { if(this.txtPasswd.Text == LoginUser(this.txtUser.Text)) { this.DialogResult = DialogResult.OK; theOwner.getUserName = this.txtUser.Text; } else { MessageBox.Show("用户名或密码错误!"); } } else { MessageBox.Show("用户名或密码错误!"); } } } private string LoginUser(string User) { SqlConnection conn = new SqlConnection("XXXXXXXX"); SqlCommand cmd = new SqlCommand(); cmd.CommandType = CommandType.StoredProcedure; cmd.CommandText = "Login"; cmd.Connection = conn; conn.Open(); SqlParameter parName = new SqlParameter("@Name",SqlDbType.VarChar,50); parName.Value = User; cmd.Parameters.Add(parName); cmd.ExecuteNonQuery(); conn.Close(); SqlDataAdapter da = new SqlDataAdapter(); da.SelectCommand = cmd; DataSet ds = new DataSet(); da.Fill(ds); return ds.Tables[0].Rows[0]["UserPasswd"].ToString(); } private bool IsUser(string User) { SqlConnection conn = new SqlConnection("XXXXXXXX"); SqlCommand cmd = new SqlCommand(); cmd.CommandType = CommandType.StoredProcedure; cmd.CommandText = "IsUser"; cmd.Connection = conn; conn.Open(); SqlParameter parName = new SqlParameter("@UserName",SqlDbType.VarChar,50); parName.Value = User; cmd.Parameters.Add(parName); cmd.ExecuteNonQuery(); conn.Close(); SqlDataAdapter da = new SqlDataAdapter(); da.SelectCommand = cmd; DataSet ds = new DataSet(); da.Fill(ds); int n; n = ds.Tables[0].Rows.Count; if(n > 0) return true; else return false; } } } 在Form2中我设计了一个label ,让它接受从Form1传过来的UserName,这样我们在以后的设计中好判断用户的权限的大小。 Form2的代码: using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; using System.Data.SqlClient; namespace login { /// <summary> /// Form2 的摘要说明。 /// </summary> public class Form2 : System.Windows.Forms.Form { private string UserName; private System.Windows.Forms.Label label1; /// <summary> /// 必需的设计器变量。 /// </summary> private System.ComponentModel.Container components = null; public Form2() { // // Windows 窗体设计器支持所必需的 // InitializeComponent(); Form1 myForm = new Form1(); myForm.ShowDialog(this); // // 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() { this.label1 = new System.Windows.Forms.Label(); this.SuspendLayout(); // // label1 // this.label1.Location = new System.Drawing.Point(128, 72); this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(152, 40); this.label1.TabIndex = 0; // // Form2 // this.AutoScaleBaseSize = new System.Drawing.Size(6, 14); this.ClientSize = new System.Drawing.Size(520, 357); this.Controls.Add(this.label1); this.Name = "Form2"; this.Text = "Form2"; this.Load += new System.EventHandler(this.Form2_Load); this.ResumeLayout(false); } #endregion [STAThread] static void Main() { Application.Run(new Form2()); } private void Form2_Load(object sender, System.EventArgs e) { this.SetDesktopLocation(200,180); this.label1.Text = UserName; } public string getUserName { get { return UserName; } set { UserName = value; } } } }
󰈣󰈤
 
 
 
>>返回首页<<
 
 热帖排行
 
 
 
静静地坐在废墟上,四周的荒凉一望无际,忽然觉得,凄凉也很美
©2005- 王朝网络 版权所有