代码之家  ›  专栏  ›  技术社区  ›  Tom

VB.NET版窗体关闭时应用程序触发关闭事件

  •  1
  • Tom  · 技术社区  · 15 年前

    我有一个VB.NET版应用程序的行为是奇怪的(或者可能根本不奇怪,我只是错过了一些东西)。

    但是,当我显示主窗体并关闭登录窗体时,应用程序正在触发关闭事件。

    这是因为应用程序认为登录窗体是唯一打开的窗体,从而引发关闭事件吗?

    当我调用我。关上()结束时触发关闭事件。我做的事情有问题吗?我以前在VB6中这样做没有任何问题(我知道他们有很多不同)。

    注意,这在frmMain中也不存在,无论我尝试打开哪个窗体,都会发生这种情况。

    谢谢。

    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 回复  |  直到 15 年前
        1
  •  3
  •   msarchet    15 年前
    If iLoginResult = 0 Then
            If Me.cmbEnvironment.Text = "Development" Then modGeneral.gbIsProduction = False
            frmMain.Show()
            Me.Close()
        End If
    

    这就是我们要做的。您正在从登录窗体打开MainForm(frmMain),因此当您关闭登录窗体时,MainForm将被释放,从而导致程序结束。

    您应该做的是从其他启动对象打开登录窗体和主窗体。

    进一步解释

    Sub Main(ByVal args() As String) 你可以这样做

    <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
    
        2
  •  3
  •   Hans Passant    15 年前

        3
  •  1
  •   SKG    15 年前

    是否在登录表单中创建/实例化主表单??。如果是,那么关闭登录窗体也将关闭主窗体..这将导致应用程序关闭。

    我建议您在主程序中打开登录窗体,然后根据响应,在主例程中实例化主窗体并使用它。

    我在我的应用程序中使用类似的东西。

     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