2010-09-10 205 views
1

我有一个VB.NET应用程序,表现奇怪(或者可能并不奇怪,我只是缺少一些东西)。VB.NET应用程序触发关闭窗体关闭事件

我有一个登录表单,当用户单击确定并成功登录时,它会加载主应用程序表单。

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

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

下面是登录例程的代码,当我最后调用Me.Close()时是关闭事件被触发时。我是不是在做坏事?我曾经在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
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 
+0

或者调用Me.Hide()而不是Me.Close() – asawyer 2010-09-10 17:53:44

+0

因此,无论您指定解决方案的启动形式是什么将用于触发关闭事件?我在应用程序启动事件中有初始化的东西,你说的是移动到其他地方并做类似Sub Main的事情?对不起,VB.NET是一种新的东西。 – Tom 2010-09-10 17:54:22

+0

@Tom,是的,再次检查我的编辑 – msarchet 2010-09-10 18:02:35

1

你创建/实例化登录形式??内的主要形式。如果是,那么关闭登录窗体也会关闭主窗体..这将导致应用程序关闭。

我建议你在主程序中打开登录表单,然后基于响应,在Main例程中实例化主表单并使用它。

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

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 
+0

是的,请参阅我上面提出的评论。 – Tom 2010-09-10 17:54:46

+0

@Tom +1我同意msarchet的评论和回答。你也可以创建一个模块并将其放置在该Main()中。只需将StartUp对象提交为Sub Main(应用程序应检测到此内容并将其与其他表单一起显示在下拉列表中,如果不只是构建)。 – SKG 2010-09-10 18:25:27

2

它是VB.NET中的一个简单修复:Project + Properties,Application选项卡。将关机模式更改为“最后一张表格关闭时”。