What's New
Q & A
Tip Jar
C# Helper...
Follow VBHelper on Twitter
MSDN Visual Basic Community
TitleProvide a login form in VB .NET
Keywordsform, login form, password form
CategoriesControls, Software Engineering
In VB 6, you can make the login form the startup form. It validates the user's user name and password, and displays the program's main form if appropriate. The login form then unloads. The program keeps running until all forms have unloaded.

This doesn't work in VB .NET. Instead, you can start from a Main subroutine. This routine displays a login form. That form returns DialogResult.OK or DialogResult.Cancel depending on whether the user name and password are valid. If the form returns DialogResult.OK, Sub Main displays the program's main form. Note that it displays the form as a dialog so Sub Main doesn't continue executing until that form is closed. If it displayed the form non-modally, Sub Main would continue running, reach the End Sub statement, and the whole program would exit. (Try it and see.)

Sub Main()
    ' Display the login dialog.
    Dim dlg As New frmLogin
    If dlg.ShowDialog() = DialogResult.OK Then
        ' The user correctly logged in.
        ' Display the main form.
        Dim frm As New frmMain
    End If
End Sub
The following code shows how the login form works.

At design time, the form's AcceptButton and CancelButton properties were set to btnOK and btnCancel. That lets those buttons fire when the user presses Return or Escape.

When the user clicks the Cancel button, the code sets the form's DialogResult property to DialogResult.Cancel. That automatically closes the form and returns DialogResult.Cancel to the calling routine (if the form is displayed using the ShowDialog method).

When the user clicks the OK button, the code does some simple validation of the user name and password. If the user name and password combination is valid, the code sets the form's DialogResult property to DialogResult.OK. That automatically closes the form and returns DialogResult.OK to the calling routine.

Private Sub btnCancel_Click(ByVal sender As Object, ByVal e _
    As System.EventArgs) Handles btnCancel.Click
    DialogResult = DialogResult.Cancel
End Sub

Private Sub btnOK_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles btnOK.Click
    ' Make sure the user entered something.
    If txtUserName.Text.Length = 0 Then
        MsgBox("You must enter a user name", _
    ElseIf txtPassword.Text.Length = 0 Then
        MsgBox("You must enter a password", _
    ElseIf PasswordInvalid(txtUserName.Text, _
        txtPassword.Text) Then
        ' The user name/password is invalid.
        MsgBox("User name/password invalid", _
        ' The user name/password is valid.
        DialogResult = DialogResult.OK
    End If
End Sub

' Put real validation here.
Private Function PasswordInvalid(ByVal user_name As String, _
    ByVal password As String) As Boolean
    Return (user_name <> password)
End Function
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.