Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Progressbars are usually an easy task but in my case I need to be able to set my maximum value of the progressbar to a label value that is undetermined untill the user makes a drive selection from my combobox. the minimum value is of course zero but when it comes time to increment the value of the progressbar the values of how much it increments by is unknown because the value of the label is summing the size in KB's of each file that is enumerated.

My thoughts were that it should be possible to calculate a progressbars percentage if you have the values as i do but how can I read the actual value from a label and not just the length which is useless. I have tried to say "Progressbar1.maximum = label13.text.tostring" but my labels value is dimed as long which causes an error arithmic overflow.


Here is the code I have to calculate used hard drive space and also the code that calculates the individual file size to sum it up in a total file size label.


Calculate Used hard drive space: Selected Index Changed event
VB
Dim totalsize As Long
        Dim freespace As Long
        Dim usedspace As Long

 Dim drives As DriveInfo() = DriveInfo.GetDrives()
            For Each drive As DriveInfo In drives
                If drive.IsReady Then
                    pathToSearchCombo.Items.Add(drive.Name)
                    totalsize = drive.TotalSize
                    freespace = drive.AvailableFreeSpace
                    usedspace = totalsize - freespace
                    Label13.Text = usedspace.ToString + " " + "byte(s)"
                End If

Calculate and add file size to label:
VB
Dim filesum As Long
For Each fi As FileInfo In fse.Matches
filesum = fi.Length + filesum
Label17.Text = filesum.ToString + " " + "byte(s)"
exit for


How can I use the values returned to the labels to increment the progressbar properly and to calculate bytes to percentage?


Thank you very much in advance!!
Posted
Updated 30-Jun-12 14:37pm
v3
Comments
Dale 2012 30-Jun-12 23:43pm    
any ideas or input?
Sergey Alexandrovich Kryukov 1-Jul-12 2:19am    
Not clear what seems to be a problem.
--SA

1 solution

Some wrong things here, let's see.

First of all, why would you ever pervert the flow of data here and try to get any data from the label?! A label can be a sink of data, not a source of it, so why trying to make its text a source? Where all the data comes? From the application, which is, in your case, the used space calculation. Where it comes? To some control which displays the data. Isn't it obvious and simple?

Then, you need such control which can be notified on some value and display this value on screen. The application part, the code using this control should be totally agnostic about how this value is shown, it should just notify it.

So, the control should totally encapsulate the presentation issue and surface only the numeric properties used to notify the control on some value to be shown. It means that the exposed properties should be pretty much the same as the one of a progress bar, such as minimum, maximum and current value, only numeric ones. This way, you can create either a custom control or a user control with the progress bar and a label in it as a child controls. Those children should not be exposed to the application code. The setter of the value property should take the value, pass it to the progress bar and format it into string to be presented in the label. Always do it, even if the application is very simple and the control is used only in one application. That's it.

Now, you should not use repeated string concatenation as you do in the calculation of the label texts. The strings are immutable, so this is a performance leak. Do I even need to explain why? Use string.Format instead:
http://msdn.microsoft.com/en-us/library/system.string.format.aspx[^].

In many other cases using format is impossible (such as in loops appending strings), then you can use mutable System.Text.StringBuilder:
http://msdn.microsoft.com/en-us/library/system.text.stringbuilder.aspx[^].

Good luck,
—SA
 
Share this answer
 
Comments
Dale 2012 1-Jul-12 3:06am    
Ok but even if I use string format and or system text stringbuilder, I am still lost as to what your saying when it comes to setting the values of the progressbar min and max. I can go over the code to make corrections for performance but right now its not my issue. Is there any easy way to display the progress while enumberating the filesystem?
Sergey Alexandrovich Kryukov 1-Jul-12 3:59am    
This is not a matter of performance per se, but a matter of culture. If you do something wrong, it's wrong. And never leave such things "for tomorrow".

What do you mean be "easy way"? What's not clear in my answer?

Did you ever heard about separation of concerns? First, develop a method of showing some progress the way you want. Later, apply it to enumerating or whatever else. Don't mess up the two together.
--SA
Dale 2012 1-Jul-12 3:13am    
my problem is that I cannot determine the values because depending on what computer the program is run on the values will always be different.

What other values can I use to come close or be exact?
Sergey Alexandrovich Kryukov 1-Jul-12 4:01am    
I don't understand the problem. What do you want to show? Free space vs. total? Or percentage of completeness of some task? In first case, you have it from the very beginning. In second case, you may or may not have the volume of the task, then the progress bar is simply not applicable.
--SA
Dale 2012 1-Jul-12 3:57am    
Is it not possible to use a background worker and set the report progress to some how drive my progressbar?..... please let me know if that is possible

thank you

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900