带有百分比标签问题的进度栏 [英] Progressbar with percent label Problem

查看:115
本文介绍了带有百分比标签问题的进度栏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

进度条通常是一件容易的事,但就我而言,我需要能够将进度条的最大值设置为一个不确定的标签值,直到用户从我的组合框中选择驱动器为止.最小值当然是零,但是到了增加进度条的值的时候,它递增多少的值是未知的,因为标签的值是每个枚举文件的KB大小的总和.

我的想法是,如果您像我一样拥有值,那么应该有可能计算进度条百分比,但是我如何才能从标签中读取实际值,而不仅仅是无用的长度.我试图说"Progressbar1.maximum = label13.text.tostring",但是我的标签值一直变暗,这会导致错误的运算溢出.


这是我必须计算使用的硬盘空间的代码,也是计算单个文件大小以将其汇总到总文件大小标签中的代码.


计算已用硬盘空间:所选索引已更改事件

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

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:

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!!

推荐答案

有些错误的地方,让我们看看.

首先,为什么您会在这里扭曲数据流并尝试从标签中获取任何数据?标签可以是数据的接收者,而不是数据的来源,那么为什么要尝试使文本成为数据的来源呢?所有数据都在哪里?在应用程序中,这是您所用的空间计算.它在哪里?给某些控件显示数据.它不明显又简单吗?

然后,您需要这样的控件,该控件可以在某个值上通知并在屏幕上显示该值.在应用程序部分,使用此控件的代码应完全不知道该值的显示方式,而应仅通知它.

因此,控件应该完全封装表示问题,并且仅浮出用于在要显示的某些值上通知控件的数值属性.这意味着公开的属性应该与进度条之一几乎相同,例如最小值,最大值和当前值,仅是数字值.这样,您可以使用进度条和其中的标签作为子控件来创建自定义控件或用户控件.这些孩子不应暴露于应用程序代码. value属性的设置器应获取值,将其传递到进度条,并将其格式化为字符串以显示在标签中.即使应用程序非常简单并且控件仅在一个应用程序中使用,也要始终这样做.就这样.

现在,您不应像在计算标签文本时那样使用重复的字符串串联.字符串是不可变的,因此这是性能泄漏.我什至需要解释为什么吗?使用string.Format代替:
http://msdn.microsoft.com/en-us/library/system.string. format.aspx [^ ].

在许多其他情况下,使用格式是不可能的(例如在循环中添加字符串),那么您可以使用可变的System.Text.StringBuilder:
http://msdn.microsoft.com/en-us/library/system.text. stringbuilder.aspx [ ^ ].

祝你好运,
—SA
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


这篇关于带有百分比标签问题的进度栏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆