|
-
Nov 25th, 2003, 02:48 PM
#1
Thread Starter
Addicted Member
Show main form after login form
I need help in the following situation. Here's what I am trying to do. In the Computer Property Startup page, I set the frmLogin as the start up object. Then in this frmLogin form, the user will have to type in his/her user name and password. If it is valid, then dispose the frmLogin form and then load the frmMain form to the user. If the authentication failed then show the frmLogin again. Here's the pseudocode I have.
If the authentication failed then
close the connection and prompt the error messagebox
If the authentication succeed then
Dim MainForm As New frmMain
MainForm.Show()
frmLogin.Dispose()
End If
But this doesn't work. I see the frmMain popup very quick then the whole program ended. What's my problem here?
Any help is appreciated!
ljCharlie
-
Nov 25th, 2003, 03:24 PM
#2
This is a pretty common problem you should try searching. Use 'login form' or something like that in the search and you should get plenty of answers.
-
Nov 25th, 2003, 03:31 PM
#3
Thread Starter
Addicted Member
Thanks! I'll try that.
ljCharlie
-
Nov 26th, 2003, 05:28 AM
#4
Hyperactive Member
Try this
Hey why donnt you first hide the login form and then show the main form. After that you can do the cleanup work for login form(if any) and finally Dispose.
Since hide (rmLogin.Hide) is fast, the user will not notice the delay.
Best of Luck !!!!
-
Nov 26th, 2003, 06:10 AM
#5
Addicted Member
Hi,
This is a common problem in .NET oriented applications. Here you have declared the frmMain in local scope.
Dim MainForm As New MainForm.
So the scope of this variable is within the login form only. The MainForm will be visible and keep loaded till the login form exists. When the loginForm ends the variables and objects assiciated within the scope of login form will go for garbage collection.
-Jai.
See you,
-Jai
[Friends Never Say Good Bye]
-
Nov 26th, 2003, 08:47 AM
#6
Thread Starter
Addicted Member
Thank you very much for the responses. In a way I agree with Jaiboy. I don't want to load the frmMain form together with the frmLogin form if the user failed to authenticate. If there is a chance that the use may provide the wrong information then I don't want to even load the frmMain form in the begining anway. If the user provide the valid authentication then go ahead and load the frmMain. But this doesn't work.
ljCharlie
-
Nov 26th, 2003, 09:33 AM
#7
I wonder how many charact
You need to have the first point of entry for your program to be a Shared Sub Main in a class file. You set in your project properties (right-click your project name in the Solution explorer), the Sub Main as the startup object.
In the Main sub, you first dim x as new loginForm. Do a LoginForm.ShowDialog (thank Edneesis), and the LoginForm will throw back the result of the Dialog.
Now, in my program, we just pass back a class instance that includes a userNominee (person trying to log on), and the result of the Login Form.
The Sub main actually checks (with our server) if the user is valid. if he's not, it never: declares a new MainMenuForm and execute an Application.Run on it.
So what you really need is a Startup class with a Public Shared Sub Main, a loginForm, and a class that encompasses a login nominee and a dialog result.
If the userNominee passes authentification, then declare a new MainMenuForm and do application.Run(myMainMenuForm)
Last edited by nemaroller; Nov 26th, 2003 at 09:43 AM.
-
Nov 26th, 2003, 10:09 AM
#8
Thread Starter
Addicted Member
Yes, nemaroller, I have one person responded with that example and it does work.
ljCharlie
-
Nov 26th, 2003, 10:30 AM
#9
I wonder how many charact
In the frmLogin (login form):
The user types in the username and password. At this point, they either hit Cancel or Ok. The code snippets below belong in frmLogin. The first one handles Cancel, the second Ok, the third is the function that returns our custom frmLoginDialogResult class instead of just a standard Windows.Forms.DialogResult. (so we declare it as shadows).
Note: Before you can use any of this code, you must code a User class (see last part of message):
VB Code:
'this code goes in your login form
'for when user clicks cancel
Private Sub btn_Cancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Cancel.Click
Me.DialogResult = DialogResult.Cancel
End Sub
'for when user clicks Ok
Private Sub btn_Login_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Login.Click
_LoginUser = New User
_LoginUser.UserName = Me.tb_Username.Text.Trim
_LoginUser.PassWord = Me.tb_Password.Text.Trim
Me.DialogResult = Windows.Forms.DialogResult.OK
End Sub
'this function is called from our Main Sub. It calls
'the standard Windows.Forms.Dialogresult which is a thread
'that waits for Me to be set to a particular dialogresult. You can
'see we set the result in the two code snippets above. As soon
'as a user clicks Ok, we set the result = DialogResult.Ok, then
'the rest of this function's code executes.
Public Shadows Function ShowDialog() As FrmLoginDialogResult
Dim myresult As Windows.Forms.DialogResult
myresult = MyBase.ShowDialog() 'code waits here for a DialogResult
Return New FrmLoginDialogResult(_LoginUser, myresult) 'pass user and result
End Function
The class used to encompass a user and the frmLogin dialog result. This class is passed back from the frmLogin.ShowDialog function to our Sub Main.
VB Code:
Imports System.Windows.Forms
Public Class FrmLoginDialogResult
Private _user As User
Private _result As DialogResult
Public Sub New()
End Sub
Public Sub New(ByVal [user] As User, ByVal [result] As DialogResult)
_user = [user]
_result = [result]
End Sub
Public ReadOnly Property User() As User
Get
Return _user
End Get
End Property
Public ReadOnly Property Result() As DialogResult
Get
Return _result
End Get
End Property
End Class
Our Sub Main:
VB Code:
Public Sub Main()
Dim frmLogin as New frm_Login 'create instance of Login form
Dim myFrmLoginResult As New FrmLoginDialogResult 'instance of our custom class
Dim UserNominee As User 'create instance of our user class
'this code shows a login form, and gets the result
'which is our custom class encompassing a user
'and a dialogResult (Ok, Cancel)
myFrmLoginResult = frmLogin.ShowDialog
'if user hit cancel, exit program
If myFrmLoginResult.Result = DialogResult.Cancel Then
Return 'ends program
End If
'get usernominee from our encompassing custom class
UserNominee = myFrmLoginResult.User 'set usernominee
'
'here you pass the UserNominee.Name & UserNominee.Password
'to whatever server or webservice, or code that
'actually checks authentification
'if they pass, then
Application.Run(MainMenuForm)
End Sub
Now, you also need to declare a User Class that exposes a name and password. Here's our code:
VB Code:
Public Class User
Private _username As String
Private _password As String
Public Property UserName() As String
Get
Return _username
End Get
Set(ByVal Value As String)
_username = Value
End Set
End Property
Public Property PassWord() As String
Get
Return _password
End Get
Set(ByVal Value As String)
_password = Value
End Set
End Property
So we're clear, the program starts in the Main, and not a form.
Last edited by nemaroller; Nov 26th, 2003 at 10:53 AM.
-
Nov 26th, 2003, 01:47 PM
#10
Thread Starter
Addicted Member
Thank you very much for the code.
ljCharlie
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|