Help with Password Form

zavsam25

New member
Joined
Aug 19, 2006
Messages
1
Programming Experience
Beginner
Hello,

I am new to VB.NET and I need help with my application. I have created a Password dialog box and would like to use it at the start of my application. When the correct password is entered, the entire application closes. How do I fix it so once the password is entered it will continue to the next part of my app and not close? Do I need to use the calling method?

Thanks
 
Your problem is almost certainly that you have made the login form your startup object. The default behaviour is to close the application when the startup form closes. What you should do is make your main form the startup form. To do this you select your project in the Solution Explorer and click the Properties button at the top. Go to the Application tab and select your main form from the "Startup form" drop-down list.

Now you need to display your login form BEFORE the main form is created. To do this, click the "View Application Events button on the same Application tab. Now select (MyApplication Events) from the drop-down list at the top left of the code window. Select Startup from the top right drop-down list and the IDE will create an event handler for the application's Startup event. This event is raised before the main form is created so you can ask the user to login here and then cancel the application if they fail. The event handler should look something like this:
VB.NET:
Private Sub MyApplication_Startup(ByVal sender As Object, _
                                  ByVal e As Microsoft.VisualBasic.ApplicationServices.StartupEventArgs) Handles Me.Startup
    Using lf As New LoginForm
        If lf.ShowDialog() <> DialogResult.OK Then
            'Exit the application without creating a main form.
            e.Cancel = True
        End If
    End Using
End Sub
Now in your login form you need to validate the user's credentials and only return OK if they are valid. You keep prompting the user to enter valid credentials until they do or else they press the Cancel button. On your login form you would have two TextBoxes named something like userNameText and passwordText and two Buttons named something like okButton and cancelLoginButton. In the designer you would assign the okButton to the form's AcceptButton property and the cancelLoginButton to the CancelButton property. You would double click the okButton to create a Click event handler then add code something like this:
VB.NET:
Private Sub okButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles okButton.Click
    If Me.ValidateCredentials Then
        'Dismiss the dialogue and indicate success.
        Me.DialogResult = Windows.Forms.DialogResult.OK
    Else
        'Prompt the user to enter valid credentials.
        MessageBox.Show("Please enter a valid user name and password.", _
                        "Invalid Credentials", _
                        MessageBoxButtons.OK, _
                        MessageBoxIcon.Warning)
    End If
End Sub

Private Function ValidateCredentials() As Boolean
    'Validate the user's credentials here and return True if they pass or False if they fail.
End Function
I'll leave the implementation of the ValidateCredentials function up to you.
 
Set the Shutdown Mode in application settings to "When last form closes" and show main form only if user login is ok.
 
Set the Shutdown Mode in application settings to "When last form closes" and show main form only if user login is ok.
I don't see that as appropriate because that means that you can open another form and close the main form and the application will not exit. That may be desirable but I doubt it. If you leave the shutdown bahaviour as is and make the main form the startup form you get the best of both worlds. You can display the login form before creating the main form, plus you can ensure that the app closes if the user closes the main form.
 
Back
Top