我有一個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
或者調用Me.Hide()而不是Me.Close() – asawyer 2010-09-10 17:53:44
因此,無論您指定解決方案的啓動形式是什麼將用於觸發關閉事件?我在應用程序啓動事件中有初始化的東西,你說的是移動到其他地方並做類似Sub Main的事情?對不起,VB.NET是一種新的東西。 – Tom 2010-09-10 17:54:22
@Tom,是的,再次檢查我的編輯 – msarchet 2010-09-10 18:02:35