1
votes

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

3 Answers

3
votes
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
3
votes

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

1
votes

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