Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Closing open file dialog before proceeding

Posted on 2004-08-25
10
Medium Priority
?
605 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
[X]
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
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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
 
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 86

Accepted Solution

by:
Mike Tomlinson earned 500 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
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…
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…
Suggested Courses

722 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