Solved

Closing open file dialog before proceeding

Posted on 2004-08-25
10
452 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
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…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

867 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

23 Experts available now in Live!

Get 1:1 Help Now