Solved

Closing open file dialog before proceeding

Posted on 2004-08-25
10
437 Views
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
Me.OpenFileDialog1.Dispose()
        LoadFile(file)

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.

Cheers
DRB2k2
0
Comment
Question by:drb2k2
  • 3
  • 2
  • 2
  • +3
10 Comments
 
LVL 76

Expert Comment

by:GrahamSkan
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.
0
 

Author Comment

by:drb2k2
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.

cheers
DRB2k2
0
 
LVL 11

Expert Comment

by:Babycorn-Starfish
ID: 11891143
You using Visual Basic .NET?
0
 
LVL 18

Expert Comment

by:JR2003
ID: 11891149
drb2k2,

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

    Me.Refresh

This will redraw the form and remove the open dialog.

JR
0
 
LVL 76

Expert Comment

by:GrahamSkan
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
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 8

Expert Comment

by:bukko
ID: 11891618
Try a 'DoEvents' after calling the ShowOpen method.
0
 
LVL 8

Expert Comment

by:bukko
ID: 11891632
OpenFileDialog is definitely a .NET class
0
 

Author Comment

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

DRB2k2
0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 125 total points
ID: 11892190
drb2k2,

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
            LoadFile(file)
        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
        Next
    End Sub

Regards,

Idle_Mind
0
 

Author Comment

by:drb2k2
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 vb.net i'll split the points between you.
Cheers
DRB2k2
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

708 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now