Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

A UserControl to log a user into an application

0.00/5 (No votes)
1 Jul 2002 1  
Demonstrates a UserControl for logging a user into an application

Introduction

This is a Form hosting the UserControl shown in the VS.NET designer

This is the UserControl shown in the shown in the VS.NET designer

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

namespace ActiveXLoginFrm
{
    /// <summary>

    /// Summary description for FrmLogin.

    /// </summary>

    public class FrmLogin : System.Windows.Forms.UserControl
    {
        private System.Windows.Forms.GroupBox groupBox1;
        private System.Windows.Forms.Label Title;
        private System.Windows.Forms.Label lblUid;
        private System.Windows.Forms.Label lblpass;
        private System.Windows.Forms.PictureBox pictureBox1;
        private System.Windows.Forms.PictureBox pictureBox2;
        private System.Windows.Forms.TextBox TxtUid;
        private System.Windows.Forms.TextBox TxtPass;
        private System.Windows.Forms.Button BtnLog;
        private System.Windows.Forms.Button BtnQuit;
        private System.Windows.Forms.PictureBox PicLogo;

        SqlConnection cnn = new SqlConnection();
        SqlDataAdapter sda;
        DataSet ds;
        DataTable dt;
        DataRow dr;

        string Empty = "";

        private char pchar = '*';
        private string logo = @"c:\ActiveXLoginFrm\MSN.ICO";
        private string lblTitle = "MAHI Software";
        private Color acol = SystemColors.Desktop;
        private Font fnt = new System.Drawing.Font(
            "Microsoft Sans Serif", 13.8F, 
            System.Drawing.FontStyle.Bold, 
            System.Drawing.GraphicsUnit.Point, 
            ((System.Byte)(0))
        );
        private Form frm;
        private Form Current;
        private string srv="webserver";
        private string db="HitenDB";
        private string ps="hiten";
        private string uid="hiten";
        /// <summary> 

        /// Required designer variable.

        /// </summary>

        private System.ComponentModel.Container components = null;
        
        public FrmLogin()
        {
            // This call is required by the Form Designer.

            InitializeComponent();

            // TODO: Add any initialization after 

            // the InitForm call

        }

        /// <summary> 

        /// Clean up any resources being used.

        /// </summary>

        protected override void Dispose( bool disposing )
        {
            if( disposing )
            {
                if(components != null)
                {
                    components.Dispose();
                }
            }
            base.Dispose( disposing );
        }

        #region Component Designer generated code
        /// <summary> 

        /// Required method for Designer support - do not modify 

        /// the contents of this method with the code editor.

        /// </summary>

        private void InitializeComponent()
        {
            System.Resources.ResourceManager resources = 
                new System.Resources.ResourceManager(
	            typeof(FrmLogin)
            );
            this.groupBox1 = 
                new System.Windows.Forms.GroupBox();
            this.Title = new System.Windows.Forms.Label();
            this.lblUid = new System.Windows.Forms.Label();
            this.lblpass = new System.Windows.Forms.Label();
            this.pictureBox1 = 
                new System.Windows.Forms.PictureBox();
            this.pictureBox2 = 
                new System.Windows.Forms.PictureBox();
            this.TxtUid = new System.Windows.Forms.TextBox();
            this.TxtPass = new System.Windows.Forms.TextBox();
            this.BtnLog = new System.Windows.Forms.Button();
            this.BtnQuit = new System.Windows.Forms.Button();
            this.PicLogo = 
                new System.Windows.Forms.PictureBox();
            this.groupBox1.SuspendLayout();
            this.SuspendLayout();
            // 

            // groupBox1

            // 

            this.groupBox1.Controls.AddRange(
                new System.Windows.Forms.Control[] {
                    this.PicLogo,
                    this.BtnLog,
                    this.TxtUid,
                    this.pictureBox1,
                    this.lblUid,
                    this.Title,
                    this.lblpass,
                    this.pictureBox2,
                    this.TxtPass,
                    this.BtnQuit
                }
            );
            this.groupBox1.Dock = 
            System.Windows.Forms.DockStyle.Fill;
            this.groupBox1.Name = "groupBox1";
            this.groupBox1.Size = 
            new System.Drawing.Size(408, 200);
            this.groupBox1.TabIndex = 0;
            this.groupBox1.TabStop = false;
            // 

            // Title

            // 

            Title.BackColor = 
            System.Drawing.SystemColors.Desktop;

            Title.Dock = System.Windows.Forms.DockStyle.Top;
            Title.Font = new System.Drawing.Font(
                "Microsoft Sans Serif", 13.8F, 
                System.Drawing.FontStyle.Bold, 
                System.Drawing.GraphicsUnit.Point, 
                ((System.Byte)(0))
            );

            Title.ForeColor = 
            System.Drawing.SystemColors.Control;
            Title.Location = new System.Drawing.Point(3, 18);
            Title.Name = "Title";
            Title.Size = new System.Drawing.Size(402, 30);
            Title.TabIndex = 1;
            Title.Text = "MAHI Software";
            Title.TextAlign = 
            System.Drawing.ContentAlignment.MiddleCenter;

            // 

            // lblUid

            // 

            this.lblUid.AutoSize = true;
            this.lblUid.Font = new System.Drawing.Font(
                "Microsoft Sans Serif", 7.8F, 
                System.Drawing.FontStyle.Bold, 
                System.Drawing.GraphicsUnit.Point, 
                ((System.Byte)(0))
            );

            this.lblUid.ForeColor = 
            System.Drawing.SystemColors.Desktop;
            this.lblUid.Location = 
            new System.Drawing.Point(144, 72);
            this.lblUid.Name = "lblUid";
            this.lblUid.Size =
            new System.Drawing.Size(52, 15);
            this.lblUid.TabIndex = 2;
            this.lblUid.Text = "User ID";
            // 

            // lblpass

            // 

            this.lblpass.AutoSize = true;
            this.lblpass.Font = new System.Drawing.Font(
                "Microsoft Sans Serif", 7.8F, 
                System.Drawing.FontStyle.Bold, 
                System.Drawing.GraphicsUnit.Point, 
                ((System.Byte)(0))
            );
            this.lblpass.ForeColor = 
            System.Drawing.SystemColors.Desktop;
            this.lblpass.Location = 
            new System.Drawing.Point(144, 115);
            this.lblpass.Name = "lblpass";
            this.lblpass.Size = 
            new System.Drawing.Size(66, 15);
            this.lblpass.TabIndex = 2;
            this.lblpass.Text = "Passwrod";
            // 

            // pictureBox1

            // 

            this.pictureBox1.Image = 
                ((System.Drawing.Bitmap)(resources.GetObject(
                    "pictureBox1.Image")
                )
            );
            this.pictureBox1.Location = 
            new System.Drawing.Point(368, 66);
            this.pictureBox1.Name = "pictureBox1";
            this.pictureBox1.Size = 
            new System.Drawing.Size(24, 26);
            this.pictureBox1.SizeMode = 
            PictureBoxSizeMode.StretchImage;
            this.pictureBox1.TabIndex = 3;
            this.pictureBox1.TabStop = false;
            // 

            // pictureBox2

            // 

            this.pictureBox2.Image = 
                ((System.Drawing.Bitmap)(resources.GetObject(
                    "pictureBox2.Image"))
            );
            this.pictureBox2.Location = new Point(368, 112);
            this.pictureBox2.Name = "pictureBox2";
            this.pictureBox2.Size = new Size(24, 24);
            this.pictureBox2.SizeMode = 
            PictureBoxSizeMode.StretchImage;
            this.pictureBox2.TabIndex = 3;
            this.pictureBox2.TabStop = false;
            // 

            // TxtUid

            // 

            this.TxtUid.Font = new Font(
                "Microsoft Sans Serif", 7.8F, 
                System.Drawing.FontStyle.Bold, 
                System.Drawing.GraphicsUnit.Point, 
                ((System.Byte)(0))
            );
            this.TxtUid.Location = new Point(216, 68);
            this.TxtUid.Name = "TxtUid";
            this.TxtUid.Size = new Size(144, 22);
            this.TxtUid.TabIndex = 4;
            this.TxtUid.Text = "";
            // 

            // TxtPass

            // 

            this.TxtPass.Font = new System.Drawing.Font(
                "Microsoft Sans Serif", 7.8F, 
                System.Drawing.FontStyle.Bold, 
                System.Drawing.GraphicsUnit.Point, 
                ((System.Byte)(0))
            );
            this.TxtPass.Location = new .Point(216, 112);
            this.TxtPass.Name = "TxtPass";
            this.TxtPass.PasswordChar = '*';
            this.TxtPass.Size = new Size(144, 22);
            this.TxtPass.TabIndex = 4;
            this.TxtPass.Text = "";
            // 

            // BtnLog

            // 

            this.BtnLog.FlatStyle = FlatStyle.Popup;
            this.BtnLog.Font = new System.Drawing.Font(
                "Microsoft Sans Serif", 7.8F, 
                System.Drawing.FontStyle.Bold, 
                System.Drawing.GraphicsUnit.Point, 
                ((System.Byte)(0))
            );
            this.BtnLog.ForeColor = SystemColors.Desktop;
            this.BtnLog.Location = new Point(210, 160);
            this.BtnLog.Name = "BtnLog";
            this.BtnLog.Size = new Size(74, 24);
            this.BtnLog.TabIndex = 5;
            this.BtnLog.Text = "&Logon";
            this.BtnLog.Click += 
            new System.EventHandler(this.BtnLog_Click);
            // 

            // BtnQuit

            // 

            this.BtnQuit.FlatStyle = FlatStyle.Popup;
            this.BtnQuit.Font = new System.Drawing.Font(
                "Microsoft Sans Serif", 7.8F, 
                System.Drawing.FontStyle.Bold, 
                System.Drawing.GraphicsUnit.Point, 
                ((System.Byte)(0))
            );
            this.BtnQuit.ForeColor = SystemColors.Desktop;
            this.BtnQuit.Location = new Point(294, 160);
            this.BtnQuit.Name = "BtnQuit";
            this.BtnQuit.Size = new Size(74, 24);
            this.BtnQuit.TabIndex = 5;
            this.BtnQuit.Text = "&Quit";
            this.BtnQuit.Click += 
            new System.EventHandler(this.BtnQuit_Click);
            // 

            // PicLogo

            // 

            this.PicLogo.Image = 
                ((System.Drawing.Bitmap)(resources.GetObject(
                "PicLogo.Image"))
            );
            this.PicLogo.Location = new Point(16, 64);
            this.PicLogo.Name = "PicLogo";
            this.PicLogo.Size = new Size(112, 96);
            this.PicLogo.SizeMode = 
                PictureBoxSizeMode.StretchImage;
            this.PicLogo.TabIndex = 6;
            this.PicLogo.TabStop = false;
            // 

            // FrmLogin

            // 

            this.Controls.AddRange(new Control[] {
                this.groupBox1 }
            );
            this.Name = "FrmLogin";
            this.Size = new System.Drawing.Size(408, 200);
            this.Load += 
	        new EventHandler(this.FrmLogin_Load);
            this.groupBox1.ResumeLayout(false);
            this.ResumeLayout(false);

        }
        #endregion

        private void FrmLogin_Load(object sender, EventArgs e)
        {
            
        }

        public string Server
        {
            get
            {
                return srv;
            }
            set
            {
                srv = value;
            }
        }

        public string DataBase
        {
            get
            {
                return db;
            }
            set
            {
                db = value;
            }
        }

        public string Password
        {
            get
            {
                return ps;
            }
            set
            {
                ps = value;
            }
        }

        public string UserID
        {
            get
            {
                return uid;
            }
            set
            {
                uid = value;
            }
        }

        public Font TitleFont
        {
            get
            {
                return fnt;
            }
            set
            {
                fnt = value;
                Title.Font = fnt;
            }
        }

        public Color AllColor
        {
            get
            {
                return acol;
            }
            set
            {
                acol = value;
                Title.BackColor = acol;
                lblUid.ForeColor = acol;
                lblpass.ForeColor = acol;
                BtnLog.ForeColor = acol;
                BtnQuit.ForeColor = acol;
            }
        }

        public string LoginTitle
        {
            get
            {
                return lblTitle;
            }
            set
            {
                lblTitle = value;
                Title.Text = lblTitle;
            }
        }

        public char PassChar
        {
            get
            {
                return pchar;
            }
            set
            {
                pchar = value;
                TxtPass.PasswordChar = pchar;
        
            }
        }

        public string LogoImage
        {
            get
            {
                return logo;
            }
            set
            {
                logo = value;
                PicLogo.Image = Image.FromFile(logo);
            }
        }

        public Form ShowForm
        {
            get
            {
                return frm;
            }
            set
            {
                frm    = value;
            }
        }

        public Form CurrentForm
        {
            get
            {
                return Current;
            }
            set
            {
                Current    = value;
            }
        }

        private void BtnLog_Click(object sender, EventArgs e)
        {
            string s =  "Initial Catalog=" + db +
	        ";Data Source=" + srv + ";UID=" + uid +
                ";pwd=" + ps + "";
            cnn.ConnectionString = s;
            cnn.Open();

            if(TxtUid.Text == Empty || TxtPass.Text == Empty)
            {
                MessageBox.Show(
                    "UserID or Password can not left blank!",
                    "Login Fail", MessageBoxButtons.OK,
                    MessageBoxIcon.Error
                );
                TxtUid.Focus();
            }
            else
            {
                sda = new SqlDataAdapter(
                    "Select * from Logon where userid = '" + 
                    TxtUid.Text + "'", cnn
                );
                ds = new DataSet();
                sda.Fill(ds);
                dt = ds.Tables[0];
                if(dt.Rows.Count == 0)
                {
                    MessageBox.Show(
                        "UserID dose not exists!", 
                        "Login Fail", MessageBoxButtons.OK, 
                        MessageBoxIcon.Error
                    );
                }
                else
                {
                    dr = dt.Rows[0];
                    string pass = dr[1].ToString();
                    if(TxtPass.Text == pass)
                    {
                        Current.Visible = false;
                        frm.Show();
                    }
                    else
                    {
                        MessageBox.Show(
                            "Password dose not match " + 
			    "with user " + TxtUid.Text ,
			    "Login Fail", 
			    MessageBoxButtons.OK,
			    MessageBoxIcon.Error
			);
                    }
                }
            }
            cnn.Close();
        }

        private void BtnQuit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
    }
}
Property Description
PassChar for setting password character.
LogoImage for setting your project logo
LoginTitle for setting our Login form title
AllColor for setting Title, LblUID, LblPass, BtnLogon, and BtnQuit's Foreground and Background Color
TitleFont for setting Title Label font setting
Server for passing server name where your database is residing
DataBase for passing database name which you want to connect to
UserID for connection to database
Password for connection to database
ShowForm Form to show after entering correct user ID and password
CurrentForm Form to hide after the correct user ID and password is entered

Note:

  • Here u must have to create object with LOGON name in database. Here is an example of such a script to create the Logon Table
    Create Table LOGON
    (
      UserID Varchar(20) PrimaryKey,
      Pass   Varchar(20)
    )
  • You then have to enter usernames and password into the table. Here is an example of the correct SQL command to do so
    Insert into LOGON Values('hpatel4u','helloindia')

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here