Windows Forms: LoginForm and MainForm
-
Monday, June 14, 2010 7:17 PM
Currently, I have a LoginForm that authenticates a user and it works fine. I have a MainForm, which is displayed if a valid username and password is entered. I have done this by using the following code in my LoginForm:
Private Sub OK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK.Click Dim ValidUser As Boolean // Authentication code in which I set ValidUser to either True or False If ValidUser Then Dim mainForm As MainForm = New MainForm() mainForm.Show() Me.Hide() End If End SubNow this does exactly what I want except I fear it's a work around and it's not the best approach. Also, note that I am using Me.Hide() to hide the LoginForm, is there a way to show the MainForm and then close the LoginForm instead of hiding it? Doesn't Me.Hide() call creates an overhead?
Thanks in advance.
Answers
-
Monday, June 14, 2010 8:10 PM
I would prefer to show ONLY the login form first and if the user is authenticated, then show main form.
In Application Events:
Friend Authenticated As System.Boolean = False Private Sub Application_Startup( _ ByVal sender As System.Object, _ ByVal e As Microsoft.VisualBasic.ApplicationServices.StartupEventArgs) _ Handles Me.Startup Dim MyLoginForm As New LoginForm 'Only changes Authenticated to TRUE if the Form returns OK If MyLoginForm.ShowDialog = DialogResult.OK Then Me.Authenticated = True End If End Sub
In MyLoginForm, ButtonClick Event:
'Do your authentication-fu If AuthenticationAlgorithmBoolean = True Then Me.DialogResult = DialogResult.OK Else Me.DialogResult = DialogResult.Cancel End If Me.Close
In MainForm.Load Event:
If My.Application.Authenticated = False Then Me.Close End If
It never hurts to try. In a worst case scenario, you'll learn from it.- Proposed As Answer by XI Samuel IX Tuesday, June 15, 2010 5:28 AM
- Marked As Answer by DaNuGai Tuesday, June 15, 2010 3:37 PM
All Replies
-
Monday, June 14, 2010 7:37 PM
If you get a dozen replies I bet you get a dozen methods. None of them are really workarounds, although I would not recommend making your Login Form and the Application's Main Form (not the same as your class named MainForm) be the same Form.
Click Project -> Properties and select the Application Tab and click Application Events. Instructions how to use Application Events are found there.
My recommendation is to make your MainForm class be the Main Form in the application, then display the Login Form as a modal dialog box from Application.StartUp. As part of your checking in the LoginForm, make its own DialogResult return OK if the user authenticates and CANCEL if the user does not authenticate.
At that point you can kill the application in its own startup routine a number of ways. My recommendation is to make an Application Scoped Boolean value indicating whether the application should kill or not, and then use the Load Event in whatever form is set as the Application Main Form to check that Boolean; this way you can make the MainForm close itself (application terminate) or simply disable all its own controls and redisplay the Login Form based on the switch value.
It never hurts to try. In a worst case scenario, you'll learn from it. -
Monday, June 14, 2010 7:40 PMModerator
Do you wish to have the main form be visible in the background while your login form authenticates the user, or do you want to only show the login form and then show the mail form once the user is authenticated?
Matt Kleinwaks - MSMVP MSDN Forums Moderator - www.zerosandtheone.com -
Monday, June 14, 2010 8:02 PM
I would prefer to show ONLY the login form first and if the user is authenticated, then show main form.
-
Monday, June 14, 2010 8:10 PM
I would prefer to show ONLY the login form first and if the user is authenticated, then show main form.
In Application Events:
Friend Authenticated As System.Boolean = False Private Sub Application_Startup( _ ByVal sender As System.Object, _ ByVal e As Microsoft.VisualBasic.ApplicationServices.StartupEventArgs) _ Handles Me.Startup Dim MyLoginForm As New LoginForm 'Only changes Authenticated to TRUE if the Form returns OK If MyLoginForm.ShowDialog = DialogResult.OK Then Me.Authenticated = True End If End Sub
In MyLoginForm, ButtonClick Event:
'Do your authentication-fu If AuthenticationAlgorithmBoolean = True Then Me.DialogResult = DialogResult.OK Else Me.DialogResult = DialogResult.Cancel End If Me.Close
In MainForm.Load Event:
If My.Application.Authenticated = False Then Me.Close End If
It never hurts to try. In a worst case scenario, you'll learn from it.- Proposed As Answer by XI Samuel IX Tuesday, June 15, 2010 5:28 AM
- Marked As Answer by DaNuGai Tuesday, June 15, 2010 3:37 PM
-
Monday, June 14, 2010 8:15 PM
I think I know what you are saying but would you be able to provide some sample code for me to see? Just to clear a few things up.
Thanks again.
-
Monday, June 14, 2010 8:34 PM
I don't have the faintest idea what more I can do for you. All you have to do is copy/paste into the right places, and I told you what the right places are too.I think I know what you are saying but would you be able to provide some sample code for me to see? Just to clear a few things up.
Thanks again.
It never hurts to try. In a worst case scenario, you'll learn from it. -
Tuesday, June 15, 2010 3:38 PM
Thanks Andrew, I appreciate all your help.

