Link to home
Start Free TrialLog in
Avatar of liljegren
liljegrenFlag for Sweden

asked on

Progess bar in a file loading process

VB6:

Hello experts. I posted a question about loading big files. Here's another related one, which I choose to make its own question of.

I'm making a program which loads (sometimes) big textfiles into a textbox. Now I want to add a progressbar to the form, to make users feel better when loading big files.

The question is: How can add a progressbar to the file loading? That is, what parameter should the bar be connected to, and what is the maximum value?

Here's the code I use for importing the files:

    CommonDialog1.ShowOpen
    sFile = CommonDialog1.FileName
    If sFile <> "" Then
        iFn = FreeFile
        Open sFile For Input As #iFn
        Do While Not EOF(iFn)
            Line Input #iFn, sLine
            txtInput = txtInput & sLine & vbCrLf
        Loop
        Close #iFn
    End If
Avatar of AzraSound
AzraSound
Flag of United States of America image

if youre reading the lines in then all you need is to find out how many lines you have and divide it by how many you have read in thus far and get ther percentage complete to display in a progress bar.  This code will tell you how many lines are in your file:

Function Count_Lines_In_File (ByVal strFilePath As String) As Integer
                    'delcare variables
                    Dim fileFile As Integer
                    Dim intLinesReadCount As Integer
                    intLinesReadCount = 0
                    'open file
                    fileFile = FreeFile


                    If (File_Exists(strFilePath)) Then
                        Open strFilePath For Input As fileFile
                    Else
                        'file doesn't exist
                        MsgBox "File: " & strFilePath & " hasn't been downloaded yet. Preprocessing is being aborted.", MB_OK, "File Does Not Exist"
                        Count_Lines_In_File = -1
                        Exit Function
                    End If
                    'loop through file
                    Dim strBuffer As String


                    Do While Not EOF(fileFile)
                        'read line
                        Input #fileFile, strBuffer
                        'update count
                        intLinesReadCount = intLinesReadCount + 1
                    Loop
                    'close file
                    Close fileFile
                    'return value
                    Count_Lines_In_File = intLinesReadCount
                End Function

ASKER CERTIFIED SOLUTION
Avatar of Éric Moreau
Éric Moreau
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mark2150
mark2150

If you pull the entire file into the text box in one go you'll not be able to show a progress bar as you're only performing one INPUT statement and all of delay is occurring there.

Me, I'd opt for one big file pull and just pop a "Please wait, loading". Message while things run.

M
Avatar of liljegren

ASKER

I was afraid of something like that too, mark2150, but fortunately you are wrong in this case. :-) emoreau's solution was the best answer I could get. Thank you all.
Emoreau:
ProgressBar1.Value = Len(txtInput)
...
ProgressBar1.Value = Len(txtInput) _
     + ProgressBar1.Value
...
<smile> and a <wink>



       
Doing it as one big load will be faster than doing a series of Line Input statements, esp. with a *big* file. For me, I'd rather have a static message displayed for a shorter period of time than a dynamic message displayed for a longer time.

Also, be sure to make the textbox.visible = false to keep the system from attempting to redraw the box as the data comes in. This too, will greatly slow the process down.

Since the two approaches are easy to code, why not write it both ways and *time* them loading the same large file. I think you may be surprised at the time difference.

M