How do you write a 'login' winform?

I have a quick question:

I have a winform that needs to have a 'login' window.  I would like to show the login window before the main form opens.  If the login fails, the whole thing should close; otherwise, the login window should hide and the main window should become visible.

I can't seem to get this to work.  All I am ending up with is the two forms opened one on top of the other.

Does anyone know of a place I could get some info on how to do this   I realize this is very simple, I am just getting started with C# and .net and would highly appreciate any ideas...

thanks,
joe


Answer this question

How do you write a 'login' winform?

  • jobethm

    i agree with flyte...
    you just have to start you app with your main form...

    on the main form load event, popup your login screen and in your login screen, authenticate the username and password... if it is correct just close the login screen, if not just keep displaying the login screen. if the user click cancel, then just put this code Application.Exit();

    eg:
    in the main window
    private formmain_loadevent(......)
    {
        frmlogin openlogin = new frmlogin();
        openlogin.ShowDialog();
    }



    do this in your login window
    private buttonOK_onclickevent(......)
    {
    if correct username and password
    this.close;
    else
    do nothing
    }

    private buttonCancel_onclickevent(......)
    {
    Application.Exit();
    }

  • Chace

    Sorry, just noticed that you are doing C#, all of this is in VB, but you should get the gist of how to do it anyway
  • wy125

    Yes, the form_load event is a great place to do it.. but I had to do it outside of the form (main window) in the Main() becuase depending on the login parameters, I had to change the appearance of my main window. 
  • Douglas R

    a couple of things first, you need to load the dialog box on load, but if they don't log in, you close the app, but you can't close an app from the load function.  So what i did is add a timer to the main form, give it a time of about 1 second, and when the timer ticks it closes the app, but then i disabled the timer.  just so you know that also you need to create a form to capture the username and password and set the login button, and cancel button to a dialog result.  No code is needed in the login form.

    Dim Login As New Login 'make a reference to the login form

    dim logged as boolean = false 'variable to check against in the loop

    do 'creates a loop that keeps popping up the login dialog box until the user has either 
        'cancelled out of logging in or has successfully logged in

        'opens the dialog box
        If Login.ShowDialog = DialogResult.OK Then 
            username = Login.TextBox1.Text
            password = Login.TextBox2.Text
            If AuthenticateUser(username, password) Then 'however you authenticate the user
                'do whatever
                logged=true 'tells the loop to exit b/c the user has logged in successfully
            Else
                'tell them their username/password was incorrect
            End If
        Else
             'they obviously clicked cancel or closed the form, so close the aplication
            me.closetimer.enabled = true 'this enables the timer and gives the load procedure
                                                       'enough time to finish and then the app will close
        End If
    loop while not logged

    that should be all that you need for it to work, works great for me.
    you can also put in a try, catch, end try function so that if something errors you enable the timer and the app closes.
    I'll check the post to see if you have any questions

  • Ot2

    I tried your code for the user login information. I am using vb.net and it didn't work. Well actually it compiled with errors. Saying that Dim login As New Login is not defined. I used the System.IO above the Public Class. It still didn't work. Is there anything else that I can use in order for his code to work
    I do have a submit button, but I entered the code in the form_load as you instructed.

  • Desperate Dan

    Thank you very much...  This is what I was looking for.

    regards,
    joe

  • Steven Khiem

    deltwebguy's solution seems a bit overkill to me. I simply did this in Main() before I show the main  window.

    [code]
    static void Main() 
    {
    MainWindow mainWindow = new MainWindow();

    MM_LoginDlg loginDlg = new MM_LoginDlg();
    loginDlg.ShowDialog();

    DialogResult result = loginDlg.DialogResult;
    if (result == DialogResult.OK)
    {
    Application.Run(mainWindow);
    }

    else
    {
    // Do nothing and terminate.
    }
    }
    [/code]

  • avm

    Actually you have to create a new form thats called the loginform... then only you can use the Dim login as new login

    just create a simple form with 2 textbox (username and password) and 2 buttons (submit/login/ok and cancel)

    then only you can call this form from the main window.


  • How do you write a 'login' winform?