Best way to use a login form

Posted 25 September 2008 - 03:32 PM

Hi, I have a login form that is loaded first upon program execution... It works just fine... The problem is when I add a logout to the MainForm... Everything I try I get problems... any ideas on the best way to accomplish this task? I just want one user to be able to log out so another user can log in...
Here is the code...

Replies To: Best way to use a login form

Re: Best way to use a login form

Posted 25 September 2008 - 04:16 PM

I was just thinking, it would seem the best way to accomplish the logout and log back in, would be to completely shut down the application and restart it... Is that possible? Somehow upon application exiting to recall the application, if the application was closed via the logout button that is...

The above code adds a handler to the main forms formclosed event so it is notified when the user closes it. I also check the close reason so the login form is only reshown if the user closed it. That way if the user shuts down the computer or kills it in task manager it wont display the login screen.

You other option is to turn off the Application Framework in My Project\Application. You can then set the startup form to sub main and have more control over the startup process. However I have learned to like the Application Framework, so try my suggestion first.

Re: Best way to use a login form

Posted 25 September 2008 - 05:34 PM

magicmonkey, what code would I use for the logout button on the MainForm in conjunction with your supplied code... I tried it as is, but it kept creating new instances of the MainForm each time I logged back in... Currently my logout button consists of just

Public Class LogoutClosedEventArgs
Inherits Windows.Forms.FormClosedEventArgs
Private _Logout As Boolean
Public Property Logout() As Boolean
Get
Return _Logout
End Get
Set(ByVal value As Boolean)
_Logout = value
End Set
End Property
Public Sub New(ByVal closeReason As Windows.Forms.CloseReason, ByVal Logout As Boolean)
MyBase.New(closeReason)
Me.Logout = Logout
End Sub
End Class

And finally in out login form we are going to listen for the new logout event...

Public Class MyLoginForm
Private Sub Login_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Login.Click
'Login validation code goes here
'If login was successful show Main Form, add handler to be notified when form closes and hide login form
Dim MyMainForm As New MyMainForm
AddHandler MyMainForm.LogoutClosed, AddressOf MyMainForm_LogoutClosed
Me.Hide()
MyMainForm.Show()
End Sub
Private Sub MyMainForm_LogoutClosed(ByVal sender As Object, ByVal e As LogoutClosedEventArgs)
If e.Logout Then
'Add code to reset login password
'Show Login Form again
Me.Show()
Else
Me.Close()
End If
End Sub
End Class

Now only when you call me.Logout on your main form will the login form show. If the user or system closes the main form the login form will close as well. Just thought it would be neat to get you using events... and i'm tired and thinking crazy...

The code above does work, so don't give up on me yet!

This post has been edited by magicmonkey: 25 September 2008 - 08:13 PM