找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 2428|回复: 8

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

  [复制链接]
  • 打卡等级:常驻代表
  • 打卡总天数:34
  • 打卡月天数:6
  • 打卡总奖励:9027
  • 最近打卡:2025-12-17 23:15:51

2823

主题

541

回帖

2万

积分

管理员

积分
22569
发表于 2021-3-12 22:28:28 | 显示全部楼层 |阅读模式
我在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;
}
}
}
}






工控课堂 www.gkket.com

0

主题

92

回帖

290

积分

注册会员

积分
290
发表于 2021-3-12 22:32:39 | 显示全部楼层
太生气了,无法HOLD啦 >_<......
工控课堂 www.gkket.com

0

主题

127

回帖

366

积分

注册会员

积分
366
发表于 2021-3-13 12:18:18 | 显示全部楼层
我顶,我顶,我顶顶顶
工控课堂 www.gkket.com

0

主题

87

回帖

131

积分

新手上路

积分
131
发表于 2025-11-13 18:53:49 | 显示全部楼层
楼主太会说了,字字句句都在理~
工控课堂 www.gkket.com

0

主题

79

回帖

115

积分

新手上路

积分
115
发表于 2025-11-13 19:43:44 | 显示全部楼层
打卡路过,支持优质原创内容~
工控课堂 www.gkket.com

0

主题

76

回帖

122

积分

新手上路

积分
122
发表于 2025-11-13 20:10:25 | 显示全部楼层
学到干货了,感谢分享,已火速收藏
工控课堂 www.gkket.com

0

主题

48

回帖

53

积分

新手上路

积分
53
发表于 2025-11-13 20:51:59 | 显示全部楼层
来凑个热闹,增加点人气~
工控课堂 www.gkket.com

0

主题

109

回帖

194

积分

新手上路

积分
194
发表于 2025-11-14 06:12:30 | 显示全部楼层
我先占个楼,等下再慢慢看~
工控课堂 www.gkket.com

0

主题

125

回帖

266

积分

注册会员

积分
266
发表于 2025-11-14 06:30:43 | 显示全部楼层
来凑个热闹,为楼主增加点人气!
工控课堂 www.gkket.com
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关闭

站长推荐上一条 /1 下一条

QQ|手机版|免责声明|本站介绍|工控课堂 ( 沪ICP备20008691号-1 )

GMT+8, 2025-12-22 18:54 , Processed in 0.079782 second(s), 26 queries .

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.

快速回复 返回顶部 返回列表