Closing open file dialog before proceeding

Posted on 2004-08-25
Last Modified: 2010-05-02
Hi all,
I have a program that reads in large files. At the moment once a user selects the file to load the open file dialog box stays on the screen and the program looks like its crashed until the file has finally been read. I have set up a label to display the loading progress on the main form, but this is never seen because the open file dialog doesn't dissapear.

I guess what i'm asking is:
Is there a way to make sure that the open file dialog has closed, and that the main form has focus before moving onto the next function?

i've tried
        Dim file As String = Me.OpenFileDialog1.FileName

to no avail.
I've also tried doing an infinite loop until the filename is nothing, or until the file dialog is nothing. Neither worked.

Any suggestions gratefully received.

Question by:drb2k2
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +3
LVL 76

Expert Comment

ID: 11890355
It's not completely clear what you are doing.

I suspect that you are not using the Common File Dialog because that has no Dispose method. It just closes itself when you've got the file name.

However, the classic answer is to put some DoEvents in your code, especially in the reading loop, which I suppose that you must have in order to monitor the status.

Author Comment

ID: 11890690
ok, i'll try to be a bit clearer.

I am using a standard OpenFileDialog right off the toolbar, with absolutely no modifications from me what-so-ever (save for filtering the file extension).

Whats appears to be happening is that as soon as the open button is clicked the filename is passed onto the load function BEFORE the dialog has closed.

There seems to be no way to tell if the openfiledialog class has been closed so I can't halt the load function until the dialog has dissapeared.

The files I am loading in are 3D files that specify the number of vertices they have. What I'm doing to monitor the loading is every time one is read I change the text of a label to show this.

BUT, since the openfiledialog never closes, it still has focus and the background window is never redrawn.
So what happens if after a few seconds the dialog dissapears and presents the user with a label saying "100/100 vertices read". Which is completely useless.

LVL 11

Expert Comment

ID: 11891143
You using Visual Basic .NET?
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 18

Expert Comment

ID: 11891149

Immediately after the file open dialog has closed - in the form that launched the dialog do a :


This will redraw the form and remove the open dialog.

LVL 76

Expert Comment

ID: 11891554
.NET? Yes that would account for my confusion.
dbr2k2, if that is so, you should be aware that there is a .NET area

Expert Comment

ID: 11891618
Try a 'DoEvents' after calling the ShowOpen method.

Expert Comment

ID: 11891632
OpenFileDialog is definitely a .NET class

Author Comment

ID: 11892107
ok i'll repost in the right area
thanks guys

LVL 86

Accepted Solution

Mike Tomlinson earned 125 total points
ID: 11892190

When a window closes, it doesn't actually repaint itself.  Instead, any windows behind that form are sent a paint message.  These windows repaint themselves, thus making the form that was closed appear to disappear.  If the form was the only one on the screen, it is the desktop window that repaints itself.

If you immediately launch a processor intensive loop (like loading a very large file), the main form doesn't get a chance to process the paint message that it received until after the loading is done.  

As both JR2003 and bukko have pointed out, you can force a form to repaint itself by issuing it a Refresh command and then allowing it to process the message with a call to DoEvents.  In .Net the call is actually Application.DoEvents.  Similarly, if you want the app to remain responsive and updated during the load procedure, you need to make a call to DoEvents from inside the loading loop.

Put all together, it would look something like this:

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If OpenFileDialog1.ShowDialog = DialogResult.OK Then
            Dim file As String = OpenFileDialog1.FileName
            Me.Refresh() ' tell main window to redraw itself
            Application.DoEvents() ' give it a chance to process paint message
        End If
    End Sub

    Private Sub LoadFile(ByVal fileName As String)
        ' some really long loading process...
        Dim i As Double

        For i = 0 To 10000000
            Label1.Text = "Processing Vertice " & i
            Application.DoEvents() ' keep the app responsive and updated
    End Sub



Author Comment

ID: 11902678
Superb, thank you idle mind
and to JR2003 and bukko.
Sorry should have split points but was too late. If you post on the same question in i'll split the points between you.

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
vbModal 12 75
Zip Folders Using Chilkat Routines 1 86
SQL Server 2012 to SQL Server 2016 24 57
2 Global Vars, 1 List Box 4 34
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question