Guys, please note that most of the header files and functions would be automatically loaded when you double click on the controls/forms.
Here, after double clicking on the Login button we find the following screen !!
First of all we declare a variable accessible by all the functions. So we declare it before the form initialisation and the code is as follows:
public static String uname;
First we need to know what the Login button does. When the Login button is clicked the code first checks if the given credentials are correct or not. So here we use a if else loop.
So, inside the "private void btn_login(Object sender, EventArgs e)" write the following code :
private void btn_login_Click(object sender, EventArgs e)
{
if (txt_uname.Text == "Martin" && txt_pwd.Text == "pingme")
{
uname = txt_uname.Text;
Form2 f = new Form2();
f.Show();
}
else
{
MessageBox.Show("sorry invalid username or password");
}
}
MessageBox.Show();
This is an pre-defined window which pops up when the condition satisfies.
Then run the code to check for errors. Steps are as follows:
- Click on Debug and then on Start Debugging.
- Or we can simply press the F5 button.
Similarly double click on the Clear button and write the following code:
private void btn_clear_Click(object sender, EventArgs e)
{
txt_uname.Clear();
txt_pwd.Clear();
txt_uname.Focus();
}
**Clear() : It is a predefined function that clears all the text present in the specified control.
**Focus() : It is another predefined function on using which it focuses on the specified TextBox control.
**txt_uname and txt_pwd are the Name properties of the textbox adjoining Username and textbox adjoining password respectively.
Then, double click on the Close button in the form design window and write the following code:
private void btn_close_Click(object sender, EventArgs e)
{
this.Close();
}
**Close() : It is a predefined function that closes the specified form.
For your reference if you are receiving error messages anywhere then clear all the code in the Form1.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public static String uname;
public Form1()
{
InitializeComponent();
}
private void btn_login_Click(object sender, EventArgs e)
{
if (txt_uname.Text == "Martin" && txt_pwd.Text == "pingme")
{
uname = txt_uname.Text;
Form2 f = new Form2();
f.Show();
}
else
{
MessageBox.Show("sorry invalid username or password");
}
}
private void btn_close_Click(object sender, EventArgs e)
{
this.Close();
}
private void btn_clear_Click(object sender, EventArgs e)
{
txt_uname.Clear();
txt_pwd.Clear();
txt_uname.Focus();
}
}
}
Please tell me if you receive any further errors. Thank You
Comments
Post a Comment