Solved

Using Windows Forms progress bar - windows forms app

Posted on 2013-05-22
6
232 Views
Last Modified: 2013-05-23
I have progress bar on my applications.  It should update as a for next loop progresses.

How do i do it?
0
Comment
Question by:vbnetcoder
  • 3
  • 3
6 Comments
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 39188851
If you're running in the main UI thread:
Some Loop
    ... code ...
    ProgressBar1.Value = x
    Application.DoEvents()
End Loop

Open in new window


You should move the code to a background thread though.  Again, take a look at the BackgroundWorker() control.  It has a ReportProgress() method which fires the ProgressChanged() event.  Search for it...examples abound!
0
 

Author Comment

by:vbnetcoder
ID: 39188975
how will this code know what percentage of the total was just completed.

For example if the loop is going to be 10 times each time i want to move the bar by a tenth
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 39189051
Since you know how many times your loop is going to iterate, just divide the current iteration  number by the total number and multiply by 100 (a percentage!).  Then leave your progressbar at the default maximum of 100 and set it to the current "percentage" value.

Another option is to set the Maximum property of the progressbar to your desired number then call PerformStep() against it for each one (set the Step to 1).
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:vbnetcoder
ID: 39189081
I not sure i am following you...


I have a variable name TotalCount that is the total amount of time it will loop


What would the code look like?
0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 39189094
Something like...
Dim percentage As Integer
For i As Integer = 1 to TotalCount

    ' ... some work ...

    percentage = i / TotalCount * 100
    ProgressBar1.Value = percentage ' <-- assuming default Min = 0, Max = 100
    Application.DoEvents()
Next

Open in new window

0
 

Author Closing Comment

by:vbnetcoder
ID: 39190709
ty
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This is a video describing the growing solar energy use in Utah. This is a topic that greatly interests me and so I decided to produce a video about it.
This is a video that shows how the OnPage alerts system integrates into ConnectWise, how a trigger is set, how a page is sent via the trigger, and how the SENT, DELIVERED, READ & REPLIED receipts get entered into the internal tab of the ConnectWise …

948 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

23 Experts available now in Live!

Get 1:1 Help Now