Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Where to add progress bar code

Status
Not open for further replies.

hugh999

MIS
Nov 29, 2001
129
IE
Hi

I have code that steps through and reads text files from a list box (listbox2), modifies the text, adds the text to a second list box (listbox1) and writes the modified text from the listbox1 to a new text file. I am trying to add a progress bar code to display the progress of each text file modified and written.

I have been playing with the code but the progress bar either shows no progress or shows progress after all the code has completed.

Here is my code:
Code:
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

        Dim i As Integer

        For i = 0 To ListBox2.Items.Count - 1
            ListBox1.Items.Clear()
            Dim myfile = ListBox2.Items(i)
            Dim mystream As New IO.FileStream(myfile, IO.FileMode.Open, IO.FileAccess.Read) ' Create a new file stream to hold the contents of the file 
            Dim sr As New IO.StreamReader(mystream) ' Use SR to read our new stream 
            Dim buffer As String = Nothing ' Used as a temporary holder 

            Do While sr.EndOfStream = False ' Do the loop until we have reached the end of the file 

                buffer = sr.ReadLine ' Throw the line into buffer 

                If buffer <> Nothing And buffer.StartsWith("#") Then ' If buffer is not a blank line or does not start with @

                    ListBox1.Items.Add(buffer + " " + (Regex.Match(buffer, "(\w+)").Groups(1).ToString()))
                Else
                    ListBox1.Items.Add(buffer)
                End If

            Loop

            sr.Close() ' Close the stream reader. 
            mystream.Close() ' Close the stream. 

            Dim i1 As Integer
            Dim s As String


            If ListBox1.Items.Count > 0 Then
                Dim file As New System.IO.StreamWriter(ListBox2.Items.Item(i).ToString + "_MarkedUp.txt", True, System.Text.Encoding.UTF8)
                For i1 = 0 To ListBox1.Items.Count - 1
                    s = ListBox1.Items.Item(i1)

                    file.WriteLine(s)

                Next i1
                file.Close()
            End If

        Next
        
    End Sub
Any help would be appreciated
 
I removed it from the code beacuse it was not working or should i say i was doing something wrong. That is why i posted clean code.
 
your code was probably good you just needed to add a progressbar.refesh or an applications.doevents after setting the value of it.

Even better would be to use a thread but that could be overkill.


Christiaan Baes
Belgium

My Blog
 
Here is the code with the progrees code inserted, as you see it is only 3 lines and does not work.

Code:
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

        Dim i As Integer

        For i = 0 To ListBox2.Items.Count - 1
            ProgressBar1.Maximum = ListBox2.Items.Count - 1
            ProgressBar1.Step = i
            ListBox1.Items.Clear()
            Dim myfile = ListBox2.Items(i)
            Dim mystream As New IO.FileStream(myfile, IO.FileMode.Open, IO.FileAccess.Read) ' Create a new file stream to hold the contents of the file 
            Dim sr As New IO.StreamReader(mystream) ' Use SR to read our new stream 
            Dim buffer As String = Nothing ' Used as a temporary holder 

            Do While sr.EndOfStream = False ' Do the loop until we have reached the end of the file 

                buffer = sr.ReadLine ' Throw the line into buffer 

                If buffer <> Nothing And buffer.StartsWith("#") Then ' If buffer is not a blank line or does not start with @

                    ListBox1.Items.Add(buffer + " " + (Regex.Match(buffer, "(\w+)").Groups(1).ToString()))
                Else
                    ListBox1.Items.Add(buffer)
                End If

            Loop

            sr.Close() ' Close the stream reader. 
            mystream.Close() ' Close the stream. 

            Dim i1 As Integer
            Dim s As String


            If ListBox1.Items.Count > 0 Then
                Dim file As New System.IO.StreamWriter(ListBox2.Items.Item(i).ToString + "_MarkedUp.txt", True, System.Text.Encoding.UTF8)
                For i1 = 0 To ListBox1.Items.Count - 1
                    s = ListBox1.Items.Item(i1)

                    file.WriteLine(s)

                Next i1
                file.Close()
            End If
ProgressBar1.PerformStep()
        Next
        
    End Sub

Where am i going wrong
 
add applications.doevents after progressbar1.performstep and tell me (and only me) what happens .

Christiaan Baes
Belgium

My Blog
 
nothing happens, there is no change as previous, the progress only shows the complete set of bars at the end.

Here is what i inserted

ProgressBar1.PerformStep()
Application.DoEvents()
ProgressBar1.Refresh()

(how can i tell you and only you)
 
You keep redefining the progress bar's Maximum in each iteration of the loop:

For i = 0 To ListBox2.Items.Count - 1
ProgressBar1.Maximum = ListBox2.Items.Count - 1

I think maybe it should look like this:

ProgressBar1.Maximum = ListBox2.Items.Count - 1
For i = 0 To ListBox2.Items.Count - 1


I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day!
 
great catch

actually both lines need to go above the for next

Code:
ProgressBar1.Maximum = ListBox2.Items.Count - 1
ProgressBar1.Step = i
For i = 0 To ListBox2.Items.Count - 1

Christiaan Baes
Belgium

My Blog
 
Thanks for the support

If i move both lines of code above the For Next, the progress bar does not work.

If i move the 1 line of code (ProgressBar1.Maximum = ListBox2.Items.Count - 1)above the For Next, the bar works but in a delayed state.

When i processed 5 txt files the progress bar only moved twice. The first movement was half way and the second movment was all the way.

Can this be adjusted to move for each file processed.
 
Yes

This is what i have

ProgressBar1.PerformStep()
Application.DoEvents()
ProgressBar1.Refresh()
 
Try changing ProgressBar1.Step = i to ProgressBar1.Step = [red]1[/red]


I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top