C#做的一個登陸控件,圖是用自已做的截圖軟件截的

本文介绍了一个基于 C# 开发的登录控件实现细节,包括如何验证用户名和密码,以及登录成功与失败后的处理逻辑。此外还提供了对数据库进行读取用户信息的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一個登陸控件,圖是用自已做的截圖軟件截的

 

源代碼:

using System; 
using System.Collections; 
using System.ComponentModel; 
using System.Drawing; 
using System.Data; 
using System.Windows.Forms; 
using System.Text; 
using System.Data.SqlClient;
using System.Net;

namespace CustTool

    
public delegate void Successful();
    
public delegate void Failed();

    
/// 
    
/// ctlLogin 的摘要說明。 
    
/// 
    public class CustLogin : 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 lUserno; 
        
private System.Windows.Forms.TextBox txtPassword; 
        
private System.Windows.Forms.TextBox txtUserno; 
        
private System.Windows.Forms.PictureBox pictureBox1; 
        
private System.Windows.Forms.ErrorProvider errorProvider; 
        
private ToolTip toolTip = new ToolTip();

        
/// 
        
/// 必需的設計器變量。 
        
/// 
        private System.ComponentModel.Container components = null

        [Category(
"CustomerMethod"),
        Description(
"驗證用戶名及密碼成功後的動作.")]
        
//public event System.EventHandler Successful; 
            public event Successful OnSuccessful;

        [Category(
"CustomerMethod"),
        Description(
"驗證用戶名及密碼失敗後的動作.")]
        
//public event System.EventHandler Failed; 
            public event Failed OnFailed;


        
private string connectionString="server=192.168.6.4;user id=puser;password=dye;database=productorder";
        
private ArrayList userNo=new ArrayList(); 
        
private ArrayList passWord=new ArrayList(); 
        
private ArrayList userMemo=new ArrayList(); 
        
private int userIndex=-1;
        
private string errorMessage = "請輸入正確的用戶賬號和密碼!"

        
private string userNoField="";
        
private string passWordField="";
        
private string userMemoField="";
        
private string userTableName="";


        
public CustLogin() 
        { 
            
// 該調用是 Windows.Forms 窗體設計器所必需的。 
            InitializeComponent(); 

            
// TODO: 在 InitializeComponent 調用後添加任何初始化 
            userNo.Clear(); 
            passWord.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(CustLogin));
            
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.lUserno = new System.Windows.Forms.Label();
            
this.txtPassword = new System.Windows.Forms.TextBox();
            
this.txtUserno = new System.Windows.Forms.TextBox();
            
this.errorProvider = new System.Windows.Forms.ErrorProvider();
            
this.panel1.SuspendLayout();
            
this.SuspendLayout();
            
// 
            
// panel1
            
// 
            this.panel1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
                
| System.Windows.Forms.AnchorStyles.Right)));
            
this.panel1.BackColor = System.Drawing.SystemColors.Window;
            
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(00);
            
this.panel1.Name = "panel1";
            
this.panel1.Size = new System.Drawing.Size(40052);
            
this.panel1.TabIndex = 0;
            
// 
            
// pictureBox1
            
// 
            this.pictureBox1.Image = ((System.Drawing.Image)(resources.GetObject("pictureBox1.Image")));
            
this.pictureBox1.Location = new System.Drawing.Point(3368);
            
this.pictureBox1.Name = "pictureBox1";
            
this.pictureBox1.Size = new System.Drawing.Size(3232);
            
this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
            
this.pictureBox1.TabIndex = 6;
            
this.pictureBox1.TabStop = false;
            
// 
            
// lText
            
// 
            this.lText.Location = new System.Drawing.Point(3624);
            
this.lText.Name = "lText";
            
this.lText.Size = new System.Drawing.Size(28420);
            
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(124);
            
this.lHeader.Name = "lHeader";
            
this.lHeader.Size = new System.Drawing.Size(30820);
            
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(304256);
            
this.btnLogin.Name = "btnLogin";
            
this.btnLogin.Size = new System.Drawing.Size(7020);
            
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(72224);
            
this.lPassword.Name = "lPassword";
            
this.lPassword.Size = new System.Drawing.Size(8820);
            
this.lPassword.TabIndex = 16;
            
this.lPassword.Text = "密 碼";
            
this.lPassword.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            
// 
            
// lUserno
            
// 
            this.lUserno.Font = new System.Drawing.Font("Tahoma"8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
            
this.lUserno.Location = new System.Drawing.Point(72184);
            
this.lUserno.Name = "lUserno";
            
this.lUserno.Size = new System.Drawing.Size(8820);
            
this.lUserno.TabIndex = 15;
            
this.lUserno.Text = "賬 號";
            
this.lUserno.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            
// 
            
// txtPassword
            
// 
            this.txtPassword.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            
this.txtPassword.Location = new System.Drawing.Point(168224);
            
this.txtPassword.Name = "txtPassword";
            
this.txtPassword.PasswordChar = '*';
            
this.txtPassword.Size = new System.Drawing.Size(20822);
            
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);
            
// 
            
// txtUserno
            
// 
            this.txtUserno.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            
this.txtUserno.Location = new System.Drawing.Point(168184);
            
this.txtUserno.Name = "txtUserno";
            
this.txtUserno.Size = new System.Drawing.Size(20822);
            
this.txtUserno.TabIndex = 12;
            
this.txtUserno.Text = "";
            
this.txtUserno.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.txtUserno_KeyPress);
            
this.txtUserno.Leave += new System.EventHandler(this.Control_Leave);
            
this.txtUserno.Enter += new System.EventHandler(this.Control_Enter);
            
// 
            
// errorProvider
            
// 
            this.errorProvider.ContainerControl = this;
            
// 
            
// CustLogin
            
// 
            this.BackColor = System.Drawing.SystemColors.Control;
            
this.Controls.Add(this.btnLogin);
            
this.Controls.Add(this.lPassword);
            
this.Controls.Add(this.lUserno);
            
this.Controls.Add(this.txtPassword);
            
this.Controls.Add(this.txtUserno);
            
this.Controls.Add(this.panel1);
            
this.Name = "CustLogin";
            
this.Size = new System.Drawing.Size(400280);
            
this.panel1.ResumeLayout(false);
            
this.ResumeLayout(false);

        } 
        
#endregion 

        
#region PublicProperty
        
/// 
        
/// 聯接字串
        
/// 
        [Category("DataProperty"),
        Description(
"數據庫連接字串.")]
        
public string ConnectionString
        {
            
get
            {
                
return connectionString;
            }
            
set
            {
                connectionString 
= value;
            }
        }

        
/// 
        
/// 所有用戶 
        
/// 
        [Category("CustomerProperty"),
        Description(
"所有用戶賬號."),
        Browsable(
false)]
        
public ArrayList UserNo 
        { 
            
get 
            { 
                
return userNo; 
            } 
            
set 
            { 
                userNo 
= value; 
            } 

        } 
        
/// 
        
/// 所有用戶密碼 
        
/// 
        [Category("CustomerProperty"),
        Description(
"所有用戶密碼."),
        Browsable(
false)]
        
public ArrayList PassWord 
        { 
            
get 
            { 
                
return passWord; 
            } 
            
set 
            { 
                passWord 
= value; 
            } 
        } 
        
/// <summary>
        
/// 所有個人用戶的姓名或部門用戶的部門名稱
        
/// </summary>
        [Category("CustomerProperty"),
        Description(
"所有用戶姓名或公共賬號的部門名稱."),
        Browsable(
false)]
        
public ArrayList UserMemo
        {
            
get
            {
                
return userMemo;
            }
            
set
            {
                userMemo
=value;
            }
        }

        
/// 
        
/// 在用戶數組Userno中的索引 
        
/// 
        [Category("CustomerProperty"),
        Description(
"用戶索引.")]
        
//[Browsable(false)]
        public int UserIndex 
        { 
            
get 
            { 
                
return userIndex; 
            } 
            
//set 
            
//
            
//    userIndex=value; 
            
//
        } 

        
/// 
        
/// 錯誤信息 
        
/// 
        [Category("CustomerProperty"),
        Description(
"登陸失幾時的提示信息.")]
        
public string ErrorMessage 
        { 
            
get 
            { 
                
return errorMessage; 
            } 
            
set 
            { 
                errorMessage 
= value; 
            } 
        } 
        
/// 
        
/// 表頭背景顏色
        
/// 
        [Category("CustomerProperty"),
        Description(
"表頭背景顏色.")]
        
public Color HeadBackColor
        {
            
get
            {
                
return panel1.BackColor;
            }
            
set
            {
                panel1.BackColor
=value;
            }
        }
        
/// 
        
/// 表頭前景顏色
        
/// 
        [Category("CustomerProperty"),
        Description(
"表頭前景顏色.")]
        
public Color HeadForeColor
        {
            
get
            {
                
return panel1.ForeColor;
            }
            
set
            {
                panel1.ForeColor
=value;
            }
        }
        
/// 
        
/// LOGO圖標,推薦32*32位圖
        
/// 
        [Category("CustomerProperty"),
        Description(
"LOGO圖標,推薦32*32位圖")]
        
public Image Logo
        {
            
get
            {
                
return pictureBox1.Image;
            }
            
set
            {
                pictureBox1.Image
=value;
            }
        }

        [Category(
"DataProperty"),
        Description(
"數據表中用於儲存用戶賬號的字段名稱.")]
        
public string  UserNoField
        {
            
get
            {
                
return userNoField;
            }
            
set
            {
                userNoField
=value;
            }
        }
        [Category(
"DataProperty"),
        Description(
"數據表中用於儲存用戶密碼的字段名稱.")]
        
public string  PassWordField
        {
            
get
            {
                
return passWordField;
            }
            
set
            {
                passWordField
=value;
            }
        }

        [Category(
"DataProperty"),
        Description(
"數據表中用於儲存用戶備注的字段名稱.")]
        
public string  UserMemoField
        {
            
get
            {
                
return userMemoField;
            }
            
set
            {
                userMemoField
=value;
            }
        }

        [Category(
"DataProperty"),
        Description(
"數據表中用於儲存用戶的表名稱.")]
        
public string  UserTableName
        {
            
get
            {
                
return userTableName;
            }
            
set
            {
                userTableName
=value;
            }
        }


        
#endregion 

        
protected virtual void successful()
        {
            
if(OnSuccessful!=null)
            {
                OnSuccessful();
            }
        }
        
protected virtual void failed()
        {
            
if(OnFailed!=null)
            {
                OnFailed();
            }
        }
        
private  bool LoadUser() 
        { 
            
try
            {
                userNo.Clear();
                passWord.Clear();
                userMemo.Clear();



                
string selectStr="";

                
if(this.userNoField!="")
                {
                    selectStr
=this.UserNoField+","+this.PassWordField+","+this.UserMemoField;

                }
                
else
                {
                    selectStr
=this.UserNoField+","+this.PassWordField;

                }



                SqlConnection conn
=new SqlConnection (connectionString);
                SqlCommand cmd
=new SqlCommand ("SELECT "+selectStr+" FROM "+this.UserTableName,conn);
                
if(conn.State!=ConnectionState.Open)
                {
                    conn.Open ();
                }
                SqlDataReader myReader;
                myReader 
=cmd.ExecuteReader ();
                
if (myReader.HasRows)
                {
                    
while (myReader.Read())
                    {
                        UserNo.Add(myReader.GetValue(
0).ToString());
                        PassWord.Add(myReader.GetValue(
1).ToString());
                        UserMemo.Add(myReader.GetValue(
2).ToString());
                    }
                }
                
else
                {
                    
return false;

                    
//MessageBox.Show("沒有用戶注冊");
                }
                myReader.Close();
                
if(conn.State!=ConnectionState.Closed)
                {
                    conn.Close();
                }
                
return true;
            }
            
catch
            {
                MessageBox.Show(
"連接服務器失敗! 服務器沒有啟動或您的計算機沒有與網絡連通!","系統提示",MessageBoxButtons.OK,MessageBoxIcon.Stop);
                errorProvider.SetError(btnLogin, 
"連接服務器失敗! 服務器沒有啟動或您的計算機沒有與網絡連通!"); 
                
return false;
            }

        }

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

            
if(LoadUser())
            {
                
bool b = CheckPassword(UserNo, PassWord); 
                
if (b) 
                { 
                    successful(); 
                } 
                
else 
                { 
                    failed(); 
                } 
            }

        } 

        
/// 
        
/// 判斷用戶名和密碼是否正確 
        
/// 
        
/// 所有用戶 
        
/// 所有用戶密碼 
        
/// 
        private bool CheckPassword(ArrayList alUser,ArrayList alPWD) 
        { 
            
string strUser=""
            
string strPWD=""
            
if(alUser.Count>0 && alPWD.Count>0
            { 
                
for(int i=0;i <alUser.Count;i++)
                { 
                    strUser
=alUser[i].ToString(); 
                    strPWD
=alPWD[i].ToString().Trim(); 
                    
if(strUser.Trim()==txtUserno.Text.Trim() && strPWD.Trim()==txtPassword.Text.Trim()) 
                    { 
                        userIndex
=i; 
                        errorProvider.Dispose(); 
                        
return true
                    } 
                    
if(i == alUser.Count-1
                    { 
                        errorProvider.SetError(btnLogin, ErrorMessage); 
                        toolTip.SetToolTip(btnLogin,ErrorMessage);
                        
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 txtUserno_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e) 
        { 
            
if(e.KeyChar==(char)13
            { 
                txtPassword.Focus(); 
            } 
        } 
        
private void txtPassword_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e) 
        { 
            
if(e.KeyChar==(char)13
            { 
                btnLogin_Click(sender,
new EventArgs()); 
            } 
        } 

        
    } 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值