tags:

views:

111

answers:

3

Hey all,

I have a VB.NET app that is behaving strangely (or perhaps not strangely at all and I'm just missing something).

I have a login form that when the user clicks OK and logs in successfully it loads the main application form.

However when I show the main form and close the login form, the app is firing the shutdown event.

Is this because the app thinks that the login form is the only form open and thus fires the shutdown event?

Here is the code for the login routine, when I call Me.Close() at the end is when the shutdown event is fired. Am I doing things out of order? I used to do it this way in VB6 with no problems (I know they're a lot different).

Note, it's nothing in frmMain either, this happens no matter what form I try to open.

Thanks.

Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click

    'iLoginResult = 0 : Success
    '               1 : Invalid user name or password
    '               2 : Other login error
    '               3 : User not authorized

    Dim iLoginResult As Integer = 2
    Dim sTopLabel As String = ""
    Dim sBottomLabel As String = ""

    Me.Cursor = Cursors.WaitCursor

    Try
        If Me.txtUserName.Text.ToString.Trim = "" Or Me.txtPassword.Text.ToString.Trim = "" Then
            MessageBox.Show("Enter a user name and password before continuing.", "DocGen", MessageBoxButtons.OK, MessageBoxIcon.Warning)
            Exit Try
        End If
        iLoginResult = modGeneral.bLogin(Me.txtUserName.Text.ToString.Trim, Me.txtPassword.Text.ToString.Trim)
        Select Case iLoginResult
            Case 1 : sTopLabel = "The user name or password is incorrect" : sBottomLabel = "Check your user name then type your password again."
            Case 2 : sTopLabel = "General login error" : sBottomLabel = "Contact your information technology department."
            Case 3 : sTopLabel = "Unauthorized access" : sBottomLabel = "Contact your information technology department to gain access to this system."
        End Select
        If iLoginResult > 0 Then
            RaiseDialog(sTopLabel, sBottomLabel)
            Me.txtPassword.Text = ""
            Me.txtUserName.Focus()
            Me.txtUserName.SelectAll()
        End If
    Catch ex As Exception
        RaiseError("", "frmLogin.btnOK_Click", Err.Number, Err.Description)
    End Try

    Me.Cursor = Cursors.Default

    If iLoginResult = 0 Then
        If Me.cmbEnvironment.Text = "Development" Then modGeneral.gbIsProduction = False
        frmMain.Show()
        Me.Close()
    End If

End Sub
+3  A: 
If iLoginResult = 0 Then
        If Me.cmbEnvironment.Text = "Development" Then modGeneral.gbIsProduction = False
        frmMain.Show()
        Me.Close()
    End If

This is what is doing it. You are opening the MainForm (frmMain) from the Login Form, thus when you close the login form the MainForm gets disposed, causing your program to end.

What you should be doing is opening your login form and main form from some other startup object.

Further Explanation

So By Using Sub Main(ByVal args() As String) you can do something like this

<STAThread)> _
Public Shared Sub Main(ByVal args() As String)
   Using login as New LoginForm
      If login.ShowDialog <> DialogResult.OK Then
         'End the Application or Whatever if the login isn't valid
      End If
   End Using

   frmMain.Show()

   Application.Run()
End Sub
msarchet
Or call Me.Hide() instead of Me.Close()
asawyer
So whatever you specify as the solution's startup form is what will be used to fire the shutdown event? I have initialization stuff in the application startup event, you're saying move that somewhere else and do something akin to Sub Main? Sorry, kind of new to VB.NET.
Tom
@Tom, Yes, check my edits in a second
msarchet
Where does Sub Main go in VB.NET? Can I initialize all my variables from within sub main instead of app startup and then just open the login form from sub main and take it from there?
Tom
Yea that's what I would do, I have my sub main inside of a Class that I call ProgramStartup.
msarchet
@Tom +1 I agree with msarchet comments and answer. You can also create a module and place this Main() inside that. Just mention the StartUp object as Sub Main (App should detect this and show it in the drop down along with other forms, if not just do a build).
SKG
Or I've found in the application properties I can set the shutdown mode to "when last form closes" which kind of acts as VB6 used to behave. Is this not desireable in VB.NET?
Tom
@Tom I would advice not using it, just for clarity and readability purposes. Fellow programmer might be wondering how the app closes, whereas in the Main() approach pretty much anyone can follow the logic.
SKG
@SKG, exactly VB .NET is not VB6 for a reason. (My work used to be a VB6 shop, I was brought on to assist in the new versions which are .NET and I spend a lot of time explaining facts like this)
msarchet
@Tom just let the program end out when the form closes naturally.
msarchet
I can't get the sub main way to work for me, I think I have one of those vb6 > .NET mental block things working against me right now so I'm going to go with the other shutdown mode until I can have someone explain it to me in person and show me or until I have the time to sort it out on my own. Thanks though, I appreciate you folks taking the time and I'm bookmarking this thread.
Tom
+1  A: 

Are you creating/instantiating the main form within the Login form??. If yes then closing the Login form is going to close the main form too..which will cause the app shutdown.

I suggest you open the Login Form in the main program and then based on the response, instantiate the main form in the Main routine and use it.

I use something like this in my app.

 Public Sub Main()

            If Not(LoginForm.ValidateUser()) Then
                'bail out
                Exit Sub
            End If

            'create the listing form
            mainForm = New MainForm

            'run it as the application main form
            Application.Run(mainForm )
End Sub
SKG
Yes, see the comment I made above.
Tom
@Tom +1 I agree with msarchet comments and answer. You can also create a module and place this Main() inside that. Just mention the StartUp object as Sub Main (App should detect this and show it in the drop down along with other forms, if not just do a build).
SKG
+2  A: 

It is a simple fix in VB.NET: Project + Properties, Application tab. Change Shutdown mode to "When last form closes".

Hans Passant