Solved

Progess bar in a file loading process

Posted on 2000-03-30
6
152 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 69

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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

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

12 Experts available now in Live!

Get 1:1 Help Now