INTRODUCTION
I have designed a login form in Visual Studio 2015 using Windows Form Application with C# code, wherein the username and password get validated at the entry level and would deny entry for users who have unauthenticated credentials. The objective is to be more precise for the user with his/her username and password created by the administrator and hence, could not be changed under any circumstance without prior permission from the administrator. Also, to overcome numerous attempts, the validation stops the user from not more than three attempts. If all three attempts are proven wrong, access is denied for the user and he/she has to communicate with the administrator for login. To make the login form more creative, I have added picture box which would display picture for login and whether access has been granted or not for that particular user. It can be altered with the user photo to look more attractive whenever he/she logs into the application.
Prerequisites
1. Visual Studio 2015
2. System configuration with Windows 8.1 or Windows 10
Steps to create a login page.
Step 1
Open Visual Studio 2015, navigate to File >> New, and choose Project to open a new project.
Step 2
After completing Step 1, a new window opens up with templates for which the application can be chosen. The login form is developed using Visual C# and selecting the corresponding Windows Forms Application. The selected work area could be saved in this stage by giving desired form name in Name text area. The form name and the solution name will be the same that the user types. After completing this, click OK.
Step 3
Windows Form creates a work space by displaying an empty form. The user can select Toolbox from left-top corner to bring the desired tools required for designing. I have brought the following tools inside my form - label (4 nos), textbox (2 nos), button (2 nos), and picturebox (1). Picturebox is included in login form to add creativity and it displays the corresponding pictures for users who will be logging into the system.
Step 4
Next stage is for converting the tools brought inside the form into meaningful interactives. Tools are named according to their usage by right clicking on any tool and then selecting Properties. This displays a window on right side of the screen. Select Text option and type the tool name. Label 4 is left empty to display the validating message which is inside the coding.
Step 5In the Solution Explorer, select Form1.cs which is the work space for our creation. Start to code inside the Main method. This is my code in which I have included how the tools will be reacting when the project is run.
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
-
- namespace login_form
- {
- public partial class Form1 : Form
- {
- static int attempt = 3;
- public Form1()
- {
- InitializeComponent();
- }
-
-
- private void button2_Click(object sender, EventArgs e)
- {
- this.Close();
- }
-
- private void button1_Click(object sender, EventArgs e)
- {
- string username = textBox1.Text;
- string password = textBox2.Text;
-
- if ((this.textBox1.Text == "Admin") && (this.textBox2.Text == "admin"))
- {
- attempt = 0;
- pictureBox1.Image = new Bitmap(@"C:\Users\Mic 18\Desktop\granted.jpg");
- MessageBox.Show("you are granted with access");
-
-
- }
- else if ((attempt == 3) && (attempt > 0))
- {
- pictureBox1.Image = new Bitmap(@"C:\Users\Mic 18\Desktop\images1.jpg");
- label4.Text = ("You Have Only " + Convert.ToString(attempt) + " Attempt Left To Try");
- --attempt;
- }
- else
- {
- pictureBox1.Image = new Bitmap(@"C:\Users\Mic 18\Desktop\denied.jpg");
- MessageBox.Show("you are not granted with access");
- }
-
- }
- }
- }
Step 6
The completed form looks like this and it is now running. To start the application, press F5.
The user who uses this form will type the username and desired password to enter into the next level.
Step 7
By typing the correct username and password and clicking the LOGIN button, the picture box changes the default picture with a different picture and a message box is prompted telling about the code validation so that the user can start accessing the application further.
Step 8
If the user types wrong username or password, the label under picture box displays a message with the number of attempts left out for the user to try.
Step 9
I have limited the user entry with only three attempts. The code checks for the correct entry in username and password. If all the three attempts contain either wrong username or wrong password, the user will not be allowed to continue his/her process with the application. This is shown in the below picture.
Summary
In the above steps, I have explained how to create a simple login form using C# in Windows Form Application and validating the username and password with limited attempts as well as changing the pictures according to the type of user. I have explained a scenario with only one user login. Future enhancements will be including a database with usernames and passwords where we can add any number of users and respective passwords.