下面的子程序从名为frmWelcomePage的winform运行。我的表单上有几个按钮使用该子按钮。子工作按预期工作,但我不得不在我的代码中添加一个If语句,如果条件满足,那么frmWelcomePage需要关闭/隐藏。但是,虽然If语句中的所有内容都满足条件时,我的frmWelcomePage不会关闭,它仍保持打开状态。如果我绕过这个条件,在我的Sub结束时我也要求关闭这个表格,这样就行了。我无法弄清楚为什么它在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
答案 0 :(得分:1)
行:
frmWelcomePage.Hide() 'frmWelcomePage is hidden as you coded
Dim frmClientInfo As New frmClientInformation
frmClientInfo.Show() 'frmClientInfo shows and implicitly shows its parent: frmWelcomePage
表单frmWelcomePage是frmClientInfo形式的主要表单(将其视为父表单)。您的代码隐藏了父级,但是当您询问frmClientInfo(将其视为子级)时,将自动显示父级。
尝试使用:
frmCleintInfo.ShowDialog()
答案 1 :(得分:1)
您是否尝试过反转这些线?
frmWelcomePage.Hide() 'it will not close here
Dim frmClientInfo As New frmClientInformation
frmClientInfo.Show()
到
Dim frmClientInfo As New frmClientInformation
frmClientInfo.Show()
frmWelcomePage.Hide() 'it will not close here
除非您在创建frmClientInfo
时引发了不同的事件,否则这应该有效