6 Replies - 10248 Views - Last Post: 25 September 2008 - 08:11 PM Rate Topic: -----

#1 webwired  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 33
  • View blog
  • Posts: 339
  • Joined: 26-August 07

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...

LoginForm Code
Option Strict On
Option Explicit On

Public Class LoginForm

	Inherits System.Windows.Forms.Form

	Public TID As Integer
	Public TName As String
	Public TPass As String

	Private Sub OK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK.Click
		TName = UsernameTextBox.Text.ToLower
		TPass = PasswordTextBox.Text
		Dim tempString As String = DataAccess.ValidateLogin(TName, TPass)
		Integer.TryParse(tempString, TID)
		If TID > 0 Then
			MainForm.Visible = True
		Else
			UsernameTextBox.Clear()
			PasswordTextBox.Clear()
			UsernameTextBox.Focus()
			MessageBox.Show("Incorrect username and/or password.")
		End If
	End Sub



MainForm code
Option Strict On
Option Explicit On

Public Class MainForm

	Inherits System.Windows.Forms.Form

	Public TID As Integer = LoginForm.TID
	Public TName As String = LoginForm.TName
	Public TPass As String = LoginForm.TPass

	Private Sub MainForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
		LoginForm.Close()
	End Sub



I've tried all thinkable combinations of closing and/or hiding one or the other form, but none seem to work, errggghhh! LOL

This post has been edited by webwired: 25 September 2008 - 03:46 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Best way to use a login form

#2 webwired  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 33
  • View blog
  • Posts: 339
  • Joined: 26-August 07

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...
Was This Post Helpful? 0
  • +
  • -

#3 magicmonkey  Icon User is offline

  • D.I.C Regular

Reputation: 106
  • View blog
  • Posts: 484
  • Joined: 12-September 08

Re: Best way to use a login form

Posted 25 September 2008 - 05:08 PM

If you want to still use the Application Framework you can do something like this...

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.FormClosed, AddressOf mainForm_Closed
		Me.Hide()
		MyMainForm.Show()
	End Sub

	Private Sub mainForm_Closed(ByVal sender As Object, ByVal e As FormClosedEventArgs)
		Select Case e.CloseReason
			Case CloseReason.UserClosing
				'Add code to reset login password

				'Show Login Form again
				Me.Show()
			Case Else
				'Exit application if close request is not userclosing
				Me.Close()
		End Select
	End Sub
End Class



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.
Was This Post Helpful? 0
  • +
  • -

#4 webwired  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 33
  • View blog
  • Posts: 339
  • Joined: 26-August 07

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
LoginForm.ShowDialog()
Here's the code of yours that I modified...

Option Strict On
Option Explicit On

Public Class LoginForm

	Inherits System.Windows.Forms.Form

	Public TID As Integer
	Public TName As String
	Public TPass As String

	'Private Sub LoginForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

	'End Sub

	'Private Sub OK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK.Click
	'	'MainForm.Close()
	'	TName = UsernameTextBox.Text.ToLower
	'	TPass = PasswordTextBox.Text
	'	Dim tempString As String = DataAccess.ValidateLogin(TName, TPass)
	'	Integer.TryParse(tempString, TID)
	'	If TID > 0 Then
	'		MainForm.Visible = True
	'	Else
	'		UsernameTextBox.Clear()
	'		PasswordTextBox.Clear()
	'		UsernameTextBox.Focus()
	'		MessageBox.Show("Incorrect username and/or password.")
	'	End If
	'End Sub

	Private Sub Login_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK.Click
		'Login validation code goes here
		TName = UsernameTextBox.Text.ToLower
		TPass = PasswordTextBox.Text
		Dim tempString As String = DataAccess.ValidateLogin(TName, TPass)
		Integer.TryParse(tempString, TID)
		If TID > 0 Then
			'If login was successful show Main Form, add handler to be notified when form closes and hide login form
			Dim MainForm As New MainForm
			AddHandler MainForm.FormClosed, AddressOf mainForm_Closed
			Me.Hide()
			MainForm.Show()
		Else
			UsernameTextBox.Clear()
			PasswordTextBox.Clear()
			UsernameTextBox.Focus()
			MessageBox.Show("Incorrect username and/or password.")
		End If
	End Sub

	Private Sub mainForm_Closed(ByVal sender As Object, ByVal e As FormClosedEventArgs)
		Select Case e.CloseReason
			Case CloseReason.UserClosing
				'Add code to reset login password
				TID = 0
				TName = ""
				TPass = ""
				'Show Login Form again
				Me.Show()
			Case Else
				'Exit application if close request is not user closing
				Me.Close()
		End Select
	End Sub

	Private Sub Cancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel.Click
		Me.Close()
	End Sub

End Class


This post has been edited by webwired: 25 September 2008 - 05:41 PM

Was This Post Helpful? 0
  • +
  • -

#5 magicmonkey  Icon User is offline

  • D.I.C Regular

Reputation: 106
  • View blog
  • Posts: 484
  • Joined: 12-September 08

Re: Best way to use a login form

Posted 25 September 2008 - 07:19 PM

Just do a me.close from your main form.
Was This Post Helpful? 0
  • +
  • -

#6 webwired  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 33
  • View blog
  • Posts: 339
  • Joined: 26-August 07

Re: Best way to use a login form

Posted 25 September 2008 - 07:26 PM

View Postmagicmonkey, on 25 Sep, 2008 - 07:19 PM, said:

Just do a me.close from your main form.


Yeah, I tried that, it gave an error about the MainForm being closed...

Anyway, I've moved on to try it a completely other way, by putting a panel over the program and hiding and unhiding it...
Was This Post Helpful? 0
  • +
  • -

#7 magicmonkey  Icon User is offline

  • D.I.C Regular

Reputation: 106
  • View blog
  • Posts: 484
  • Joined: 12-September 08

Re: Best way to use a login form

Posted 25 September 2008 - 08:11 PM

If you only want to show the login form again when the user clicks a logout button, I may of miss understood your requirement.

This gets a bit ugly, but its late....

First we are going to add a LogoutClosed event to our main form...

 Public Class MyMainForm

	'Logout flag, event and sub
	Private _IsLogout As Boolean
	Public Event LogoutClosed(ByVal sender As Object, ByVal e As LogoutClosedEventArgs)
	Public Sub Logout()
		_IsLogout = True
		Me.Close()
	End Sub
	Private Sub MyMainForm_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
		RaiseEvent LogoutClosed(Me, New LogoutClosedEventArgs(e.CloseReason, _IsLogout))
	End Sub

	'Logout Sample
	Private Sub butLogout_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles butLogout.Click
		Logout()
	End Sub
End Class



This is the event args for our new logout event...
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

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1