Create Simple Login Form in Windows Application C#



Login Form in Windows Application

In our last c#.net post we learnt about how to open new windows form by Show() and ShowDialog() mothods. In this c# windows application tutorial we will learn more about Show() and ShowDialog() in detail with an example of login form.

Here, We will open new form by login with valid username and password on login page. We design a login form with two textbox controls for username and password and a button control for write login code. We have taken id txtuname for username textbox and textpass for password textbox and btnlogin for login button control.

meeraacademy.com
Simple login form in windows application.

In above figure we took the login form design with two textbox controls along with button control. We took button control for write login code in click event.

private void btnlogin_Click(object sender, EventArgs e)
{
if (txtuname.Text == "")
{
   MessageBox.Show("Enter Username !!", "Meera Academy");
}
else if(txtpass.Text=="")
{
   MessageBox.Show("Enter Password !!","Meera Academy");
}
else if (txtuname.Text == "Meera" && txtpass.Text == "123")
{
   this.Hide();
   Form frm2 = new Form2();
   frm2.ShowDialog();
}
else
{
   MessageBox.Show("Invalid Credential", "Meera Academy");
}
}

If username or password incorrect it will show error message like below screen.

meeraacademy.com
Simple login form in windows application.

In above example if textbox will be blank then it shows the message like “Enter Username”. If both username and password fill up and one of will be incorrect then it will shows the message “Invalid Credential”. If username = “Meera” and Password = “123” then the main form will be hidden and open Form2 using ShowDialog() method.

If both username and password are correct then we login into system by opening form2.

meeraacademy.com
Simple login form in windows application.

We hope that this c#.net tutorial helped you to understand about create simple login form without database in windows application.


Next, c# windows application tutorial we will learn how to pass data from one from to another form.


 

Leave a Reply

Your email address will not be published. Required fields are marked *