Introduction

This Project is a simple Login Form desktop application using Windows Forms and C#.NET. It is a good resource for a beginner in C# to learn the basics of some of the common controls and the code behind the controls and to validate empty or required data types.

Building the Sample

The Solution attached here is a Visual Studio 2015 Solution but it should work on another versions of Visual Studio as well.

Description

The Solution for the problem is achieved here in four steps which we will discuss along with the code snippets and images to have a clear understanding of how the process of login is done in the solution.

Step 1: Create and design Windows form

 
 
once the design is done, proceed to step2.

Step 2: Add a class library Login.cs in your project
Right click on the Login under Soltion Explorer->Solution 'Login' and click on Add->New Item->Class Library and rename it to Login.cs
Step 3: Write all methodologies related to the login in the library file Login.cs
Here are the initial code flows as provided in the following code snippet:

 

C#
Edit|Remove
using System; 
using System.Collections.Genericusing System.ComponentModelusing System.Datausing System.Drawingusing System.Linqusing System.Textusing System.Threading.Tasksusing System.Text.RegularExpressionsusing System.Windows.Formsnamespace Login 
{ 
    class Login 
    { 
        //decalre properties 
        public string Username { getset; } 
        public string Userpassword { getset; } 
         
        //intialise  
        public Login(string user, string pass) 
        { 
            this.Username = user; 
            this.Userpassword = pass; 
        } 
        //validate string 
        private bool StringValidator(string input) 
        { 
            string pattern = "[^a-zA-Z]"; 
            if(Regex.IsMatch(input,pattern)) 
            { 
                return true; 
            } 
            else 
            { 
                return false; 
            } 
        } 
        //validate integer 
        private bool IntegerValidator(string input) 
        { 
            string pattern = "[^0-9]"; 
            if(Regex.IsMatch(input,pattern)) 
            { 
                return true; 
            } 
            else 
            { 
                return false; 
            } 
        } 
        //clear user inputs 
        private void ClearTexts(string user, string pass) 
        { 
            user = String.Empty; 
            pass = String.Empty; 
        } 
        //method to check if eligible to be logged in 
        internal bool IsLoggedIn(string user, string pass) 
        { 
            //check user name empty 
            if (string.IsNullOrEmpty(user)) 
            { 
                MessageBox.Show("Enter the user name!"); 
                return false; 
 
            } 
            //check user name is valid type 
            else if (StringValidator(user) == true) 
            { 
                MessageBox.Show("Enter only text here"); 
                ClearTexts(user,pass); 
                return false; 
            } 
            //check user name is correct 
            else 
            { 
                if (Username != user) 
                { 
                    MessageBox.Show("User name is incorrect!"); 
                    ClearTexts(user,pass); 
                    return false; 
                } 
                //check password is empty 
                else 
                { 
                    if (string.IsNullOrEmpty(pass)) 
                    { 
                        MessageBox.Show("Enter the passowrd!"); 
                        return false; 
                    } 
                    //check password is valid 
                    else if (IntegerValidator(pass) == true) 
                    { 
                        MessageBox.Show("Enter only integer here"); 
                        return false; 
                    } 
                    //check password is correct 
                    else if(Userpassword!=pass) 
                    { 
                        MessageBox.Show("Password is incorrect"); 
                        return false; 
                    } 
                    else 
                    { 
                        return true; 
                    } 
                } 
            } 
        } 
    } 
} 
 Step 4: Invoke the Login Button
In this last step we will again go to the Form1.cs file to change the code for the click event of the login button.
Code Flows for this final step is as follows which is given in the code snippet below to the code flows:

 

C#
Edit|Remove
using System; 
using System.Collections.Genericusing System.ComponentModelusing System.Datausing System.Drawingusing System.Linqusing System.Textusing System.Threading.Tasksusing System.Windows.Forms; 
 
namespace Login 
{ 
    public partial class MainForm : Form 
    { 
        public MainForm() 
        { 
            InitializeComponent(); 
            
        } 
        //Enter code here for your version of username and userpassword 
        Login login = new Login("admin""1234"); 
         
 
        private void button1_Click(object sender, EventArgs e) 
        { 
            //define local variables from the user inputs 
            string user = nametxtbox.Text; 
            string pass = pwdtxtbox.Text; 
            //check if eligible to be logged in 
            if(login.IsLoggedIn(user,pass)) 
            { 
                MessageBox.Show("You are logged in successfully"); 
            } 
            else 
            { 
                //show default login error message 
                MessageBox.Show("Login Error!"); 
            } 
        } 
 
        private void linkLabel2_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) 
        { 
            //enter your code for forget password case 
            MessageBox.Show("Under development"); 
        } 
 
        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) 
        { 
            //Enter your code for registration form of your choice 
            MessageBox.Show("Under development"); 
        } 
    } 
} 
 

 

That's it Friends!!

 

About Me

My name is Ravi Ranjan Pandey. Its my First Sample on MSDN so please provide your valuable feedback at raviranjanpandey@outlook.com . Please suggest any further improvement. Please get back to me on my email with any such information.

Thanks a lot.