Solved

Progess bar in a file loading process

Posted on 2000-03-30
6
158 Views
Last Modified: 2012-05-07
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
0
Comment
Question by:liljegren
6 Comments
 
LVL 28

Expert Comment

by:AzraSound
ID: 2671553
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

0
 
LVL 70

Accepted Solution

by:
Éric Moreau earned 200 total points
ID: 2671557
Here is a simple solution:

Private Sub Command1_Click()
Dim iFN As Integer
Dim sFile As String
Dim sLine As String

    txtInput = ""
    CommonDialog1.ShowOpen
    sFile = CommonDialog1.FileName
    If sFile <> "" Then
        iFN = FreeFile
        Open sFile For Input As #iFN
        ProgressBar1.Min = 0
        ProgressBar1.Max = LOF(iFN)
        Do While Not EOF(iFN)
            Line Input #iFN, sLine
            txtInput = txtInput & sLine & vbCrLf
            ProgressBar1.Value = Len(txtInput)
        Loop
        Close #iFN
    End If

End Sub
0
 
LVL 12

Expert Comment

by:mark2150
ID: 2671644
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
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:liljegren
ID: 2671689
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.
0
 
LVL 14

Expert Comment

by:wsh2
ID: 2671706
Emoreau:
ProgressBar1.Value = Len(txtInput)
...
ProgressBar1.Value = Len(txtInput) _
     + ProgressBar1.Value
...
<smile> and a <wink>



       
0
 
LVL 12

Expert Comment

by:mark2150
ID: 2673080
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
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VBA saving file message display 5 72
VB 6.0 printer how to align 6 67
How to measure sizes and angles in scanned images ? 3 76
Protecting vb6 & .Net code Obfuscation 18 152
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…
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…

839 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