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

How do you write a 'login' winform?
jobethm
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
wy125
Douglas R
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 do have a submit button, but I entered the code in the form_load as you instructed.
Desperate Dan
regards,
joe
Steven Khiem
[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
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.