OpenFileDialog Control Not Closing

JDYoder

Centurion
Joined
Nov 18, 2003
Messages
144
On the OpenFileDialog control, when its FileOk event is fired, I call a subroutine to do some processing on the file selected, but the dialog remains open. I can use Messageboxes and everything (which popup over top of the dialogbox) but only when my subroutine is completed and the execution returns to the FileOk event does the dialogbox close.

Why won't it close on its own and/or what can I do to close it?
 
Visual Basic:
    Private Sub ImportGetFile()
        dlgOpen.ShowDialog()
    End Sub

    Private Sub dlgOpen_FileOk(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles dlgOpen.FileOk
        Import(dlgOpen.FileName)
        MessageBox.Show("Bye")
    End Sub

    Private Sub Import(ByVal FileName As String)
        Try

            Me.Activate()
            Application.DoEvents()
            Me.Cursor = System.Windows.Forms.Cursors.WaitCursor
            dlgOpen = Nothing

            Dim results As Interfaces.UpdateResults = Me.BusLayer.ModuleSpecificOptions("AssetsScannedTruncate", Nothing)

            If results.errorNumber = 0 Then
                FormLoadedDataView()
                Dim ds As DataSet = dcMainDataControl.SearchDataView.Table.DataSet

                Dim ScannerFile As New System.IO.StreamReader(FileName)
                Dim SiteID, BuildingID, RoomID, AssetID As String

                SiteID = ScannerFile.ReadLine

                Do While Not SiteID Is Nothing
                    BuildingID = ScannerFile.ReadLine
                    RoomID = ScannerFile.ReadLine
                    AssetID = ScannerFile.ReadLine

                    Dim dr As DataRow = ds.Tables(0).NewRow
                    dr.Item("AssetID") = AssetID
                    dr.Item("SiteID") = SiteID
                    dr.Item("BuildingID") = BuildingID
                    dr.Item("RoomID") = RoomID
                    dr.Item("Scanned") = True
                    dr.Item("Processed") = False
                    ds.Tables(0).Rows.Add(dr)

                    SiteID = ScannerFile.ReadLine
                Loop
                ScannerFile.Close()
                If dcMainDataControl.DataSaveAll() = DialogResult.Yes Then
                    results = Me.BusLayer.ModuleSpecificOptions("AssetsScannedVariance", Nothing)
                    If results.errorNumber = 0 Then
                        StatusMessage("Import completed")
                    End If
                End If
            End If
            If results.errorNumber <> 0 Then StatusMessage("Database error", True, results.resultMessage)

        Catch exIO As System.IO.IOException
            StatusMessage("Error importing data.", True, exIO.Message)

        Catch ex As SystemException
            LogError(ex)
        Finally
            FormLoadedDataView()
            Me.Cursor = System.Windows.Forms.Cursors.Default

        End Try
    End Sub


FYI, the "Bye" Messagebox is just for debugging. The "Import" function takes only a few seconds, and when it's done the "Bye" message pops up (with the dialogbox still visible behind it.) After clicking "OK" to the messagebox, the dialogbox closes.
 
In your code you have
Visual Basic:
Private Sub dlgOpen_FileOk(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles dlgOpen.FileOk
        Import(dlgOpen.FileName)
        MessageBox.Show("Bye")
    End Sub
This will run during the Dialog's OK (Open) buttons click event. The dialog is only dismissed when this event has finished.
You should really check to see if they clicked ok in your ImportGetFile routine and do the import if they did.
i.e.
Visual Basic:
Private Sub ImportGetFile()
    If dlgOpen.ShowDialog() = DialogResult.OK Then
        Import(dlgOpen.FileName)
    End If
End Sub
 
Here's a quick example of an OpenFileDialog:

-----------------------------------------------------------------------------------

Private Sub btn_load_Click(....) Handles btn_load.Click


If OpenFileDialog1.ShowDialog = DialogResult.OK Then

' when user clicks 'OK" in OpenFileDialog, the code below will be executed.
' put whatever code in here.

End If
End Sub

-----------------------------------------------------------------------------------
 
Back
Top