分享
 
 
 

登陆框控件

王朝other·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

好久以前作的东西,最近拿出来溜溜,最近一直没有作应用程序方面的东西了,有点生疏! 希望我的这篇东西能对您有所帮助,大家共同探讨共同进步,此文章比较简单!是作一个通用的登陆框控件!

首先建一解决项目LoginControl,添加一个类库的项目LoginControl,然后添加用户控件ctlLogin

然后在ctlLogin.cs文件中写代码

//ctlLogin.cs

using System;

using System.Collections;

using System.ComponentModel;

using System.Drawing;

using System.Data;

using System.Windows.Forms;

using System.Text;

namespace LoginControl

{

///

/// ctlLogin 的摘要说明。

///

public class ctlLogin : System.Windows.Forms.UserControl

{

private System.Windows.Forms.Panel panel1;

private System.Windows.Forms.Label lHeader;

private System.Windows.Forms.Label lText;

private System.Windows.Forms.Button btnLogin;

private System.Windows.Forms.Label lPassword;

private System.Windows.Forms.Label lUsername;

private System.Windows.Forms.TextBox txtPassword;

private System.Windows.Forms.TextBox txtUsername;

private System.Windows.Forms.PictureBox pictureBox1;

private System.Windows.Forms.ErrorProvider errorProvider;

///

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

///

private System.ComponentModel.Container components = null;

public event System.EventHandler Successful;

public event System.EventHandler Failed;

private ArrayList strUsername=new ArrayList();

private ArrayList strPassword=new ArrayList();

private int index=-1;

private string ErrMsg = "请输入正确的用户名和密码!";

public ctlLogin()

{

// 该调用是 Windows.Forms 窗体设计器所必需的。

InitializeComponent();

// TODO: 在 InitializeComponent 调用后添加任何初始化

strUsername.Clear();

strPassword.Clear();

}

///

/// 清理所有正在使用的资源。

///

protected override void Dispose( bool disposing )

{

if( disposing )

{

if(components != null)

{

components.Dispose();

}

}

base.Dispose( disposing );

}

#region 组件设计器生成的代码

///

/// 设计器支持所需的方法 - 不要使用代码编辑器

/// 修改此方法的内容。

///

private void InitializeComponent()

{

System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(ctlLogin));

this.panel1 = new System.Windows.Forms.Panel();

this.pictureBox1 = new System.Windows.Forms.PictureBox();

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

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

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

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

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

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

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

this.errorProvider = new System.Windows.Forms.ErrorProvider();

this.panel1.SuspendLayout();

this.SuspendLayout();

//

// panel1

//

this.panel1.BackColor = System.Drawing.SystemColors.ActiveCaptionText;

this.panel1.Controls.Add(this.pictureBox1);

this.panel1.Controls.Add(this.lText);

this.panel1.Controls.Add(this.lHeader);

this.panel1.Location = new System.Drawing.Point(4, 4);

this.panel1.Name = "panel1";

this.panel1.Size = new System.Drawing.Size(480, 52);

this.panel1.TabIndex = 0;

//

// pictureBox1

//

this.pictureBox1.Image = ((System.Drawing.Image)(resources.GetObject("pictureBox1.Image")));

this.pictureBox1.Location = new System.Drawing.Point(432, 12);

this.pictureBox1.Name = "pictureBox1";

this.pictureBox1.Size = new System.Drawing.Size(40, 32);

this.pictureBox1.TabIndex = 6;

this.pictureBox1.TabStop = false;

//

// lText

//

this.lText.Location = new System.Drawing.Point(36, 24);

this.lText.Name = "lText";

this.lText.Size = new System.Drawing.Size(360, 20);

this.lText.TabIndex = 5;

this.lText.Text = "请输入您的用户名和密码.";

this.lText.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;

//

// lHeader

//

this.lHeader.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));

this.lHeader.Location = new System.Drawing.Point(12, 4);

this.lHeader.Name = "lHeader";

this.lHeader.Size = new System.Drawing.Size(384, 20);

this.lHeader.TabIndex = 4;

this.lHeader.Text = "登陆";

this.lHeader.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;

//

// btnLogin

//

this.btnLogin.FlatStyle = System.Windows.Forms.FlatStyle.Popup;

this.btnLogin.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));

this.btnLogin.Location = new System.Drawing.Point(376, 292);

this.btnLogin.Name = "btnLogin";

this.btnLogin.Size = new System.Drawing.Size(70, 20);

this.btnLogin.TabIndex = 14;

this.btnLogin.Text = "登 陆";

this.btnLogin.Click += new System.EventHandler(this.btnLogin_Click);

//

// lPassword

//

this.lPassword.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));

this.lPassword.Location = new System.Drawing.Point(76, 252);

this.lPassword.Name = "lPassword";

this.lPassword.Size = new System.Drawing.Size(80, 20);

this.lPassword.TabIndex = 16;

this.lPassword.Text = "密 码";

this.lPassword.TextAlign = System.Drawing.ContentAlignment.MiddleRight;

//

// lUsername

//

this.lUsername.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));

this.lUsername.Location = new System.Drawing.Point(76, 212);

this.lUsername.Name = "lUsername";

this.lUsername.Size = new System.Drawing.Size(80, 20);

this.lUsername.TabIndex = 15;

this.lUsername.Text = "用户名";

this.lUsername.TextAlign = System.Drawing.ContentAlignment.MiddleRight;

//

// txtPassword

//

this.txtPassword.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;

this.txtPassword.Location = new System.Drawing.Point(176, 252);

this.txtPassword.Name = "txtPassword";

this.txtPassword.PasswordChar = '*';

this.txtPassword.Size = new System.Drawing.Size(270, 21);

this.txtPassword.TabIndex = 13;

this.txtPassword.Text = "";

this.txtPassword.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.txtPassword_KeyPress);

this.txtPassword.Leave += new System.EventHandler(this.Control_Leave);

this.txtPassword.Enter += new System.EventHandler(this.Control_Enter);

//

// txtUsername

//

this.txtUsername.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;

this.txtUsername.Location = new System.Drawing.Point(176, 212);

this.txtUsername.Name = "txtUsername";

this.txtUsername.Size = new System.Drawing.Size(270, 21);

this.txtUsername.TabIndex = 12;

this.txtUsername.Text = "";

this.txtUsername.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.txtUsername_KeyPress);

this.txtUsername.Leave += new System.EventHandler(this.Control_Leave);

this.txtUsername.Enter += new System.EventHandler(this.Control_Enter);

//

// errorProvider

//

this.errorProvider.ContainerControl = this;

//

// ctlLogin

//

this.Controls.Add(this.btnLogin);

this.Controls.Add(this.lPassword);

this.Controls.Add(this.lUsername);

this.Controls.Add(this.txtPassword);

this.Controls.Add(this.txtUsername);

this.Controls.Add(this.panel1);

this.Name = "ctlLogin";

this.Size = new System.Drawing.Size(484, 348);

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

this.panel1.ResumeLayout(false);

this.ResumeLayout(false);

}

#endregion

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

{

// strUsername.Add(txtUsername.Text.Trim());

// strPassword.Add(txtPassword.Text.Trim());

}

private void txtPassword_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)

{

if(e.KeyChar==(char)13)

{

btnLogin_Click(sender,new EventArgs());

}

}

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

{

bool b = CheckPassword(Username, Password);

if (b)

{

Successful(this, new System.EventArgs());

}

else

{

Failed(this, new System.EventArgs());

}

}

///

/// 判断用户名和密码是否正确

///

/// 所有用户

/// 所有用户密码

///

private bool CheckPassword(ArrayList alUser,ArrayList alPWD)

{

string strUser="";

string strPWD="";

if(alUser.Count>0 && alPWD.Count>0)

{

for(int i=0;i { strUser=alUser[i].ToString(); strPWD=alPWD[i].ToString().Trim(); if(strUser.Trim()==txtUsername.Text.Trim() && strPWD.Trim()==txtPassword.Text.Trim()) { Index_Username=i; errorProvider.Dispose(); return true; } if(i == alUser.Count-1) { errorProvider.SetError(btnLogin, ErrMsg); return false; } } } return false; } private void Control_Enter(object sender, System.EventArgs e) { Control ctl=(Control)sender; ctl.BackColor=Color.LemonChiffon; } private void Control_Leave(object sender, System.EventArgs e) { Control ctl=(Control)sender; ctl.BackColor=Color.White; } private void txtUsername_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e) { if(e.KeyChar==(char)13) { txtPassword.Focus(); } } /// /// 所有用户 /// public ArrayList Username { get { return strUsername; } set { strUsername = value; } } /// /// 所有用户密码 /// public ArrayList Password { get { return strPassword; } set { strPassword = value; } } /// /// 在用户数组Username中的索引 /// public int Index_Username { get { return index; } set { index=value; } } /// /// 错误信息 /// public string ErrorMessage { get { return ErrMsg; } set { ErrMsg = value; } } }} 接着,您就可以编译成LoginControl.dll 再在解决项目中添加一个测试项目Test,然后在工具箱中添加/移除项中添加刚才编译的LoginControl.dll,添加一测试界面为frmTest,然后在工具箱中把ctlLogin控件拖到frmTest页面中 然后在frmTest.cs中来测试刚才的控件 //frmTest.cs using System;using System.Drawing;using System.Collections;using System.ComponentModel;using System.Windows.Forms;using System.Data;using InformationClass; //自己写的一个测试类 namespace Test{ /// /// Form1 的摘要说明。 /// public class frmTest : System.Windows.Forms.Form { private LoginControl.ctlLogin ctlLogin; /// /// 必需的设计器变量。 /// private System.ComponentModel.Container components = null; private Users objUsers=new Users(); //实例化自定义的登陆用户集合类 private User m_objUser=new User(); //实例化自定义的登陆用户类 public frmTest() { // // Windows 窗体设计器支持所必需的 // InitializeComponent(); // // TODO: 在 InitializeComponent 调用后添加任何构造函数代码 // } /// /// 清理所有正在使用的资源。 /// protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } #region Windows 窗体设计器生成的代码 /// /// 设计器支持所需的方法 - 不要使用代码编辑器修改 /// 此方法的内容。 /// private void InitializeComponent() { System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(frmTest)); this.ctlLogin = new LoginControl.ctlLogin(); this.SuspendLayout(); // // ctlLogin // this.ctlLogin.ErrorMessage = "请输入正确的用户名和密码!"; this.ctlLogin.Index_Username = -1; this.ctlLogin.Location = new System.Drawing.Point(0, 0); this.ctlLogin.Name = "ctlLogin"; this.ctlLogin.Size = new System.Drawing.Size(484, 348); this.ctlLogin.TabIndex = 0; this.ctlLogin.Failed += new System.EventHandler(this.ctlLogin_Failed); this.ctlLogin.Load += new System.EventHandler(this.Form1_Load); this.ctlLogin.Successful += new System.EventHandler(this.ctlLogin_Successful); // // frmTest // this.AutoScaleBaseSize = new System.Drawing.Size(6, 14); this.ClientSize = new System.Drawing.Size(484, 345); this.Controls.Add(this.ctlLogin); this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle; this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon"))); this.MaximizeBox = false; this.MinimizeBox = false; this.Name = "frmTest"; this.Text = "系统登陆"; this.Load += new System.EventHandler(this.Form1_Load); this.ResumeLayout(false); } #endregion /// /// 应用程序的主入口点。 /// [STAThread] static void Main() { Application.Run(new frmTest()); } private void ctlLogin_Failed(object sender, System.EventArgs e) { MessageBox.Show("登陆失败!请输入正确的用户名和密码!!","信息提示",MessageBoxButtons.OK,MessageBoxIcon.Error); } private void ctlLogin_Successful(object sender, System.EventArgs e) { m_objUser=objUsers[ctlLogin.Index_Username]; MessageBox.Show("登陆成功!","信息提示",MessageBoxButtons.OK,MessageBoxIcon.Information); } private void Form1_Load(object sender, System.EventArgs e) { objUsers=new ReadData().GetUsers(); if(objUsers.Count>0) { foreach(User objUser in objUsers) { ctlLogin.Username.Add(objUser.Name); ctlLogin.Password.Add(objUser.Password); } } } }} 运行程序 如果登陆失败就会出现

如果登陆成功
就这样,小小的一个登陆控件就作好了,希望我的这篇文章能对于刚学没有多久的.NET程序员有所帮助如果有什么不足的地方,希望您能指正,谢谢

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
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- 王朝網路 版權所有