Sunday, December 28, 2014

Different ways of creating login form in Windows Forms using C#

8:49 PM - By azeheruddin khan 0

In this article i will demonstrate, how we can create different ways of login form in windows forms application.

1)  Using Hide and Show properties of form.
2)  Using ShowDialog() Method and catching form DialogResult property of form.
3)  Running different instance of the application.

To demonstrate the sample code i am using following forms in my application

1. LoginFrom



2)  HomeForm



Lets start with our sample code.

1)  Using Hide and Show properties of form.


Program.cs

using System;
using System.Windows.Forms;

namespace LoginExample1
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            
            //Set LoginForm here
            Application.Run(new LoginForm());
        }
    }
}


LoginForm.cs

using System;
using System.Windows.Forms;

namespace LoginExample1
{
    public partial class LoginForm : Form
    {
        public LoginForm()
        {
            InitializeComponent();
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtUserID.Text.Trim()))
            {
                MessageBox.Show("Please enter User ID");
            }
            else if (string.IsNullOrEmpty(txtPassword.Text.Trim()))
            {
                MessageBox.Show("Please enter Password");
            }
            else
            {
                if (txtUserID.Text.Trim() == "ADMIN" && txtPassword.Text.Trim() == "ADMIN")
                {
                    // Here hiding the current form
                    this.Hide();

                    // Showing HomeFrom on successfull login.
                    HomeForm objHomeForm = new HomeForm();
                    objHomeForm.Show();
                   
                }
                else
                {
                    MessageBox.Show("Please enter correct User ID and Password");
                }
            }
           
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }


    }
}



HomeForm.cs

using System.Windows.Forms;

namespace LoginExample1
{
    public partial class HomeForm : Form
    {
        public HomeForm()
        {
            InitializeComponent();
        }

        //If you close the HomeForm, still application will be in running mode because we made LoginForm as hide.  
        private void HomeForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            Application.Exit();
        }
    }
}




2)  Using ShowDialog() Method and catching form DialogResult property of form.

Program.cs

using System;
using System.Windows.Forms;

namespace LoginFormExamples
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            //Set HomeForm here
            Application.Run(new HomeForm());
        }
    }
}


LoginForm.cs

  using System;
using System.Windows.Forms;

namespace LoginFormExamples
{
    public partial class LoginForm : Form
    {
        public LoginForm()
        {
            InitializeComponent();
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtUserID.Text.Trim()))
            {
                MessageBox.Show("Please enter User ID");
            }
            else if (string.IsNullOrEmpty(txtPassword.Text.Trim()))
            {
                MessageBox.Show("Please enter Password");
            }
            else
            {
                if (txtUserID.Text.Trim() == "ADMIN" && txtPassword.Text.Trim() == "ADMIN")
                {
                    // Setting DialogResult
                    this.DialogResult = DialogResult.OK;
                }
                else
                {
                    MessageBox.Show("Please enter correct User ID and Password");
                }
            }
           
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }


    }
}

HomeForm.cs

using System.Windows.Forms;

namespace LoginFormExamples
{
    public partial class HomeForm : Form
    {
        public HomeForm()
        {
            InitializeComponent();
            
        }

        private void HomeForm_Load(object sender, System.EventArgs e)
        {
            //Hidding current form and waiting for outcome of LoginForm
            this.Hide();
            LoginForm objLoginForm = new LoginForm();
            objLoginForm.ShowDialog();

            if (objLoginForm.DialogResult == DialogResult.OK)
            {
                //Showing current form on login.
                this.Show();
            }
            else
            {
                Application.Exit();
            }
        }       
    }
}



3)  Running different instance of the application.

Program.cs

using System;
using System.Windows.Forms;

namespace LoginFormExample3
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            
            Application.Run(new LoginForm());

            //Checking outcome of the LoginForm. If login Successfull then will display HomeForm
            if (CommonClass.CheckLogin == true)
            {
                Application.Run(new HomeForm());
            }
        }
    }
}


LoginForm.cs

using System;
using System.Windows.Forms;

namespace LoginFormExample3
{
    public partial class LoginForm : Form
    {       
        public LoginForm()
        {
            InitializeComponent();
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtUserID.Text.Trim()))
            {
                MessageBox.Show("Please enter User ID");
            }
            else if (string.IsNullOrEmpty(txtPassword.Text.Trim()))
            {
                MessageBox.Show("Please enter Password");
            }
            else
            {
                if (txtUserID.Text.Trim() == "ADMIN" && txtPassword.Text.Trim() == "ADMIN")
                {
                    // Setting CheckLogin = true;
                    CommonClass.CheckLogin = true;
                    this.Close();
                    this.Dispose();
                   
                }
                else
                {
                    MessageBox.Show("Please enter correct User ID and Password");
                }
            }
           
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
            this.Dispose();
        }
    }
}

HomeForm.cs

using System.Windows.Forms;

namespace LoginFormExample3
{
    public partial class HomeForm : Form
    {
        public HomeForm()
        {
            InitializeComponent();
        }
       
    }
}


CommonClass.cs


namespace LoginFormExample3
{
    public class CommonClass
    {
        public static bool CheckLogin = false;
    }
}


I have created one solution and all the sample codes are added as different project.
You can select 1 project at a time. To make project active you have to right click on that project and click on Set as Startup Project.
You can download all the sample code from below link.





About the Author

I am Azeheruddin Khan having more than 6 year experience in c#, Asp.net and ms sql.My work comprise of medium and enterprise level projects using asp.net and other Microsoft .net technologies. Please feel free to contact me for any queries via posting comments on my blog,i will try to reply as early as possible. Follow me @fresher2programmer
View all posts by admin →

Get Updates

Subscribe to our e-mail newsletter to receive updates.

Share This Post

0 comments:

adsense

© 2014 Fresher2Programmer. WP Theme-junkie converted by Bloggertheme9
Powered by Blogger.
back to top