AllTheTimeWorld.com

csharp formsCreate a login form.

Create a login form.

Close the program is the user and password are not correct

Create a login form.

Close the program is the user and password are not correct


Text of video

In this video we add a login form to the project.

Code in FormLogin:

private void btnOK_Click(object sender, EventArgs e)
{
   // Hide the login form
   this.Hide();
}
public string user()
{
    // return the value for the user id
    return txtUser.Text;
}
public string password()
{
    // Return waht was typed as the password.
    return txtPassword.Text;
}

Code in Form1:

private void Form1_Load(object sender, EventArgs e)
{
  FormLogin frm = new FormLogin();
  frm.ShowDialog();
  // Close the program if either the password is not "password"
  // or the user is not "Mario".
  if (frm.password() != "password" || frm.user() != "Mario")
  this.Close();
}

Note that this check of the password is too simple: in practice we would need a file or database and check against that.