Windows Forms: Metro Login form with SQL Server in C#

By FoxLearn 6/19/2017 9:50:15 PM   6.41K
Create Metro Login Window step by step using sql database, Metro Framework, Modern UI in C#

Step 1Click New Project, then select Visual C# on the left, then Windows and then select Windows Forms Application. Name your project "MetroLoginForm" and then click OK

metro login form

Step 2: Right click on your project select Manage NuGet Packages -> Search metro framework -> Install

install metro framework

If you don't see the metro framework in your toolbox, you can view How to download and install metro framework

Step 3: Design metro form as below

Your login form name: frmLogin

metro login form

Your main form name: frmMain

metro login form

Add an EF model to your project as below

entity framework c#

You can run below script to create a user table

CREATE TABLE [dbo].[Users](
	[UserName] [varchar](25) NOT NULL,
	[Password] [varchar](50) NULL,
 CONSTRAINT [PK_Users] PRIMARY KEY CLUSTERED 
(
	[UserName] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

Step 4: Add code to handle your form

frmMain

using System;
using System.Windows.Forms;

namespace MetroLoginForm
{
    public partial class frmMain : MetroFramework.Forms.MetroForm
    {
        bool _logOut;
        public frmMain(string userInfo)
        {
            InitializeComponent();
            lblUserInfo.Text = userInfo;
        }

        private void lnkLogOut_Click(object sender, EventArgs e)
        {
            _logOut = true;
            this.Close();
            frmLogin.Instance.Show();
        }

        private void frmMain_FormClosed(object sender, FormClosedEventArgs e)
        {
            //main form close
            if (!_logOut)
                Application.Exit();
        }
    }
}

frmLogin

using System;
using System.Data;
using System.Linq;
using System.Windows.Forms;

namespace MetroLoginForm
{
    public partial class frmLogin : MetroFramework.Forms.MetroForm
    {
        static frmLogin _instance;
        //Using singleton pattern to create an instance
        public static frmLogin Instance
        {
            get
            {
                if (_instance == null)
                    _instance = new frmLogin();
                return _instance;
            }
        }

        public frmLogin()
        {
            InitializeComponent();
        }

        private void frmLogin_Load(object sender, EventArgs e)
        {
            _instance = this;
            txtUsername.Focus();
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtUsername.Text))
            {
                MetroFramework.MetroMessageBox.Show(this, "Please enter your username.", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtUsername.Focus();
                return;
            }
            try
            {
                using(DbUserEntities db = new DbUserEntities())
                {
                    //Using linq to query data
                    var query = from u in db.Users
                                where u.UserName == txtUsername.Text && u.Password == txtPassword.Text
                                select u;
                    if (query.SingleOrDefault() != null)
                    {
                        //Hide login form, then show main form
                        this.Hide();
                        frmMain frm = new frmMain(string.Format("Login with: {0}", txtUsername.Text));
                        frm.ShowDialog();
                    }
                    else
                        MetroFramework.MetroMessageBox.Show(this, "Your username or password is incorrect.", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            catch(Exception ex)
            {
                MetroFramework.MetroMessageBox.Show(this, ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }
}

VIDEO TUTORIALS