Results 1 to 10 of 10

Thread: Show main form after login form

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Feb 2003
    Posts
    219

    Question 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

  2. #2
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    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.

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Feb 2003
    Posts
    219
    Thanks! I'll try that.

    ljCharlie

  4. #4
    Hyperactive Member sw_is_great's Avatar
    Join Date
    Nov 2003
    Posts
    330

    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 !!!!
    Regards

  5. #5
    Addicted Member
    Join Date
    Nov 2003
    Location
    India
    Posts
    227
    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]

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Feb 2003
    Posts
    219
    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

  7. #7
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    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.

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Feb 2003
    Posts
    219
    Yes, nemaroller, I have one person responded with that example and it does work.

    ljCharlie

  9. #9
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    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:
    1. 'this code goes in your login form
    2. 'for when user clicks cancel
    3. Private Sub btn_Cancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Cancel.Click
    4.         Me.DialogResult = DialogResult.Cancel
    5.     End Sub
    6.  
    7. 'for when user clicks Ok
    8. Private Sub btn_Login_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Login.Click
    9.         _LoginUser = New User
    10.         _LoginUser.UserName = Me.tb_Username.Text.Trim
    11.         _LoginUser.PassWord = Me.tb_Password.Text.Trim
    12.         Me.DialogResult = Windows.Forms.DialogResult.OK
    13.     End Sub
    14. 'this function is called from our Main Sub. It calls
    15. 'the standard Windows.Forms.Dialogresult which is a thread
    16. 'that waits for Me to be set to a particular dialogresult. You can
    17. 'see we set the result in the two code snippets above. As soon
    18. 'as a user clicks Ok, we set the result = DialogResult.Ok, then
    19. 'the rest of this function's code executes.
    20. Public Shadows Function ShowDialog() As FrmLoginDialogResult
    21.         Dim myresult As Windows.Forms.DialogResult
    22.         myresult = MyBase.ShowDialog() 'code waits here for a DialogResult
    23.         Return New FrmLoginDialogResult(_LoginUser, myresult) 'pass user and result
    24.     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:
    1. Imports System.Windows.Forms
    2. Public Class FrmLoginDialogResult
    3.     Private _user As User
    4.     Private _result As DialogResult
    5.  
    6.     Public Sub New()
    7.  
    8.  
    9.     End Sub
    10.  
    11.     Public Sub New(ByVal [user] As User, ByVal [result] As DialogResult)
    12.         _user = [user]
    13.         _result = [result]
    14.     End Sub
    15.  
    16.     Public ReadOnly Property User() As User
    17.         Get
    18.             Return _user
    19.         End Get
    20.     End Property
    21.  
    22.     Public ReadOnly Property Result() As DialogResult
    23.         Get
    24.             Return _result
    25.         End Get
    26.     End Property
    27.  
    28. End Class

    Our Sub Main:
    VB Code:
    1. Public Sub Main()
    2. Dim frmLogin as New frm_Login 'create instance of Login form
    3. Dim myFrmLoginResult As New FrmLoginDialogResult 'instance of our custom class
    4. Dim UserNominee As User 'create instance of our user class
    5.  
    6. 'this code shows a login form, and gets the result
    7. 'which is our custom class encompassing a user
    8. 'and a dialogResult (Ok, Cancel)
    9. myFrmLoginResult = frmLogin.ShowDialog
    10.  
    11. 'if user hit cancel, exit program
    12. If myFrmLoginResult.Result = DialogResult.Cancel Then
    13.    Return  'ends program
    14. End If
    15.  
    16. 'get usernominee from our encompassing custom class
    17. UserNominee = myFrmLoginResult.User 'set usernominee
    18.  
    19. '
    20. 'here you pass the UserNominee.Name & UserNominee.Password
    21. 'to whatever server or webservice, or code that
    22. 'actually checks authentification
    23. 'if they pass, then
    24. Application.Run(MainMenuForm)
    25.  
    26. End Sub


    Now, you also need to declare a User Class that exposes a name and password. Here's our code:
    VB Code:
    1. Public Class User
    2.  
    3.     Private _username As String
    4.     Private _password As String
    5.  
    6.  
    7.     Public Property UserName() As String
    8.         Get
    9.             Return _username
    10.         End Get
    11.         Set(ByVal Value As String)
    12.             _username = Value
    13.         End Set
    14.     End Property
    15.  
    16.     Public Property PassWord() As String
    17.         Get
    18.             Return _password
    19.         End Get
    20.         Set(ByVal Value As String)
    21.             _password = Value
    22.         End Set
    23.     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.

  10. #10

    Thread Starter
    Addicted Member
    Join Date
    Feb 2003
    Posts
    219

    Thumbs up

    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
  •  



Click Here to Expand Forum to Full Width