王朝网络
分享
 
 
 

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

王朝c#·作者佚名  2006-06-13
宽屏版  字体: |||超大  

我在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;

}

}

}

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
>>返回首页<<
推荐阅读
 
 
频道精选
 
静静地坐在废墟上,四周的荒凉一望无际,忽然觉得,凄凉也很美
© 2005- 王朝网络 版权所有