2010-09-10 206 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選項卡。將關機模式更改爲「最後一張表格關閉時」。