Solved

Progess bar in a file loading process

Posted on 2000-03-30
6
159 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
[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
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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 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…

749 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