Make an login windows in app.

Hello.

I am making an application that has restricted access. It is just simple.

I have an main form that where everything happens. So i just need user to type in the password to get in.

I thought just to make an window with masked textbox and button. And behind button this code:

if (maskedbutton.text == "password")

{

mainform.show();

}

else

system.close();

It does not need to be any more difficult then that. But this does not work so i need help. I cant get to my mainform from my login window and I need help with my code to close my form if password is wrong.

I have done this before and this code worked....but now now.

Do you have any suggestions



Answer this question

Make an login windows in app.

  • guywwe

    I usually do this:

    Application.Run( new LoginForm() );

    In LoginForm:

    private btnLogin_Click( sender ...... )
    {
    if( txtPassword.Text == "SomeSecretPassword" )
    {
    MainForm mForm = new MainForm();
    mForm.Show();
    this.Close();
    }
    else
    {
    MessageBox.Show( "Illegal password!" );
    txtPassword.Focus();
    }
    }

    It should work.

  • Laurent67

    hi.,

    I think you should do like this:

    Application.Run(new MainForm());

    In the MainForm:

    private MainForm_Load(sender ......)

    {

    LoginForm from = new LoginForm();

    from.ShowDialog();

    }

    In the LoginForm:

    private btnLogin_Click(sender... )

    {

    if ( passwordTextBox.Text == password )

    {

    this.Close();

    }

    else

    {

    // show some error

    passwordTextBox.Text = String.Empty;

    passwordTextBox.Focus();

    }

    }

    private btnCancel_Click(sender...)

    {

    Application.Exit();

    }

    After you login, you don't need the LoginForm. So I usually first load MainForm and then show LoginForm. If user can login, just close the login form. If can't, just exit the application.

    cheers..

    soemoe



  • Blue The Dog

    this kind of works.

    but I have two problems...

    1) mForm in your code does not close when password is correct...(this.Close(); should close it )

    2) how to remove close, maximize and minimize buttons of the loginscreen

    thank you for your help


  • Mystagogue

    Thanks.

    This helped alot:)


  • Shalin Dalal

    1. If you using ShowDialog(), LoginForm won't close. If you use Show(), it have to close.
    2. If you want to hide all buttons, set ControlBox to False in Form properties. You can set it to True and set MaximizeButton and MinimizeButton to False, because it's better choice. If user won't write down password, he has to have a chance to close appliaction.


  • Make an login windows in app.