Jose M.
Jose M.

Reputation: 2340

Winform does not close within If Statement, but does outside of it

The sub below runs from the winform called frmWelcomePage. There are several buttons on my form that use that sub. The sub works as expected, but I had to add an If statement to my code, within that statement, if the condition is met, then the frmWelcomePage needs to close/hide. However, while the everything within the If statement works when the condition is met, my frmWelcomePage does not close, it remains open. If I bypass the condition, at the end of my Sub I also ask to close this form, and that works fine. I can't figure out then why it does not work within the If statement. Here is my code:

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

Upvotes: 0

Views: 80

Answers (2)

E-r Gabriel Doronila
E-r Gabriel Doronila

Reputation: 533

Have you tried inverting these lines?

frmWelcomePage.Hide() 'it will not close here

Dim frmClientInfo As New frmClientInformation
frmClientInfo.Show()

to

Dim frmClientInfo As New frmClientInformation
frmClientInfo.Show()

frmWelcomePage.Hide() 'it will not close here

this should work unless you have a different event raised when creating the frmClientInfo

Upvotes: 1

NoChance
NoChance

Reputation: 5752

in the lines:

    frmWelcomePage.Hide()   'frmWelcomePage is hidden as you coded
    Dim frmClientInfo As New frmClientInformation
    frmClientInfo.Show()     'frmClientInfo shows and implicitly shows its parent: frmWelcomePage 

The form frmWelcomePage is the main form (think of it as the parent) of the form frmClientInfo. Your code hides the parent but when you ask the frmClientInfo (think of this as a child) to show the parent is automatically shown.

Try using:

frmCleintInfo.ShowDialog()

Upvotes: 1

Related Questions