NeoTekSystems Blog

This Blog is hosted and monitored by NeoTekSystems and its staff.
All programmers and business representatives are welcome
to stop by and participate in discussions about the .NET framework.
Welcome to NeoTekSystems Blog Sign in | Join | Help
in Search

How to create a login form class

Last post 06-07-2005, 8:18 PM by jspano. 0 replies.
Sort Posts: Previous Next
  •  06-07-2005, 8:18 PM 21

    How to create a login form class

    This FAQ will explain how to show and close a login form correctly in .NET. It will not show how to do good login form code such as limiting login tries or how to connect to a db etc. The attached project was created with VS.NET 03. If you have 02, just create a new solution and project and add the form and module files.

    There are two main points of interest in the project. frmLogin is the form class that encapsulates the login code. modMain shows how to use the class.

    Code:
    Public Enum LoginStatusENUM
        LoginOk
        LoginFailed
    End Enum
    Public Event LoginStatus(ByVal Status As LoginStatusENUM)

    Private Sub TryLogin()
        'This code should be replaced with good login code.
        If txtName.Text = "John" And txtPassword.Text = "john" Then
            Me.Close()
            'Login was ok so raise the event with a login ok status
            RaiseEvent LoginStatus(LoginStatusENUM.LoginOk)
        Else
            MessageBox.Show("Invalid Login")
        End If
    End Sub

    Private Sub btnLogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLogin.Click
        TryLogin() 'Execute the login code to check user and password
    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        'User canceled, close form and throw the event with a failed status
        Me.Close()
        RaiseEvent LoginStatus(LoginStatusENUM.LoginFailed)
    End Sub

    and the code from the module

    Code:
    Public Sub main()
        Dim frmLogMeIn As frmLogin
        frmLogMeIn = New frmLogin
        'add the handler for the event
        AddHandler frmLogMeIn.LoginStatus, AddressOf MyLoginStatus
        frmLogMeIn.ShowDialog()
    End Sub

    Public Sub MyLoginStatus(ByVal Status As frmLogin.LoginStatusENUM)
        'The user either canceled or loged in OK
        If Status = frmLogin.LoginStatusENUM.LoginOk Then
            'frmMain.ShowDialog()
            MessageBox.Show("Showing Form Main!")
        End If
        'else login failed so drop out the back of the sub to close the program
    End Sub

    The code is commented well, but I'll give a quick overview of the code. For the login form, we handle all code to see if the user loged in OK or not. We raise the event when the login status has been decided. The form gets closed either way to allow full control to pass back to the event that handles the login status. If the MyLoginStatus sub doesn't open a form modally, then the app ends.

    John Spano
    President
    NeoTekSystems, Inc.
    www.NeoTekSystems.com
    MCSD, MCTS-Windows, MCTS-Web, MCPD-Distributed, MCITP-SQLDev, MCITP-SQLAdmin
View as RSS news feed in XML
Powered by Community Server, by Telligent Systems