如何从winform中正确关闭工作簿

时间:2013-12-02 21:42:32

标签: vb.net winforms

我的工作簿上有一个表单,可以在工作簿启动事件中打开。表单检查一些内容,如果它们不存在或用户按下取消按钮,表单将关闭,工作簿也应该关闭。然而,如果满足所有条件,表格将关闭,然后打开另一个条件。这就是问题所在,我编写了一个允许表单关闭工作簿的过程,但是当表单关闭时,我的其他表单因为工作簿关闭而无法打开。我正在寻找解决此问题的方法。这是我为处理close事件而编写的代码:

Public Class ThisWorkbook

    Dim WithEvents clientForm As System.Windows.Forms.Form

    Private Sub ThisWorkbook_Startup(sender As Object, e As EventArgs) Handles Me.Startup
        Dim xlSumSheet As Excel.Worksheet = CType(ThisApplication.Worksheets("summaryWorksheet"), Excel.Worksheet)
        Dim strAppTitle As String = CStr(xlSumSheet.Range("A2").Value)

        clientForm = New frmClientInformation

        'Set the application title.
        ThisApplication.ActiveWindow.Caption = strAppTitle

        'if the there is no client information on the 
        'summary sheet, display the import form

        If strAppTitle = "" Then

            clientForm.ShowDialog()

        End If
    End Sub

    Private Sub clientForm_onClosed(ByVal sender As Object, ByVal e As System.EventArgs) Handles clientForm.FormClosed

       Globals.ThisWorkbook.Application.Quit()

    End Sub
End Class

以下是我的表单上的代码,问题出在哪里(参见粗体:)

Private Sub btnEnterName_Click(sender As Object, e As EventArgs) Handles btnEnterName.Click

   Dim strPrompt As String = "Hello " & strUserName & "." & vbNewLine & _
      "You are about to initialize the import process. Do you wish to proceed?"
   Dim strTitle As String = "Begin Installation"
   Dim msgProceed As MsgBoxResult

   msgProceed = MsgBox(strPrompt, CType(MsgBoxStyle.YesNo + MsgBoxStyle.Information, MsgBoxStyle), strTitle)

   If msgProceed = MsgBoxResult.Yes Then
      Dim importForm As New frmDataImportSplash
      Globals.refSummaryWorksheet.Range("A2").Value = "Prepared for " & txtClientInfo.Text

      importForm.Show() 'Because the WB closes, this event never happens, How do I fix this issue**
      Me.Hide()

   Else
      MsgBox("Procedured Cancelled")
      Me.Close()

   End If
End Sub

0 个答案:

没有答案