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?
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

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

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!

Question has a verified solution.

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

Introduction While answering a recent question ( in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses
Course of the Month5 days, 11 hours left to enroll

626 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