下面的子文件从名为frmWelcomePage的winform运行。我的表单上有几个按钮使用该子项。子工程按预期工作,但我必须在该语句中添加一条If语句,如果条件满足,则frmWelcomePage需要关闭/隐藏。但是,当条件满足时,If语句中的所有内容都起作用,但我的frmWelcomePage不会关闭,它仍保持打开状态。如果我绕过这个条件,在我的Sub结尾我也要求关闭这个表单,并且工作正常。我无法弄清楚为什么它在If语句中不起作用。这里是我的代码:Winform在If语句中没有关闭,但是在它之外没有关闭
Public Sub GoToSheets(sheetName As String)
'This sub is used to open the workbook on the selected sheet.
'This checks to see if Excel workbook is opened, if not it
'opens Excel, the workbook and then the selected sheet. If the workbook is
'opened, it goes to the selected sheet.
'@param sheetName, sheet to be displayed
Cursor.Current = Cursors.WaitCursor
Try
'get an existing excel.application object
xlApp = CType(GetObject(, "Excel.Application"), Application)
Catch ex As Exception
'no existing excel.application object - create a new one
xlApp = New Excel.Application
End Try
Dim xlWBName As String = "2011.1004.Compensation Template"
Dim xlBookPath As String = Path.Combine(Directory.GetCurrentDirectory())
xlApp.Visible = True
Try
'get the opened workbook
xlBook = xlApp.Workbooks(xlWBName & ".xlsx")
Catch ex As Exception
'open it
xlBook = xlApp.Workbooks.Open(xlBookPath & "\" & xlWBName & ".xlsx")
End Try
Try
xlSheet = CType(CType(xlBook.Sheets("summarySheet"), Excel.Worksheet), Excel.Worksheet)
Dim strChckRange As String = xlSheet.Range("A2").Value
If strChckRange Is Nothing Then
frmWelcomePage.Hide() 'it will not close here
Dim frmClientInfo As New frmClientInformation
frmClientInfo.Show()
Else
xlSheet = CType(CType(xlBook.Sheets(sheetName), Excel.Worksheet), Excel.Worksheet)
'close the navigation instance on the welcome page
frmNavigation.Close()
'activate requested sheet
xlSheet.Activate()
'display as dashboard
DashboardView()
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp)
GC.Collect()
GC.WaitForPendingFinalizers()
GC.Collect()
GC.WaitForPendingFinalizers()
frmWelcomePage.Hide() ' it closes here just fine
chkForm()
Cursor.Current = Cursors.WaitCursor
End If
Catch ex As Exception
End Try
End Sub
不要捕捉空异常时,提出了不同的事件。 – LarsTech
如果注释掉在if语句的真实条件中创建/显示'frmClientInformation'的另外两行,会发生什么?我正在工作,如果它是与表单之间的父/子关系有关的东西...(也许一个孩子不能显示没有显示父母?) –