VB.NET 进度条百分比
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14243225/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
VB.NET Progress bar percentage
提问by Alex
I created a progress bar which works perfectly. I recently added percentages but I'd like to display the label on top of the progress bar.
我创建了一个完美运行的进度条。我最近添加了百分比,但我想在进度条顶部显示标签。
Like so:
像这样:


The only problem as you can see is that the background is not transparent. Dispite having:
您可以看到唯一的问题是背景不透明。尽管有:
lblPercentage.BackColor = Color.Transparent
on form load... Is there something that can be done for this?
表单加载...有什么可以做的吗?
回答by Olivier Jacot-Descombes
The Transparent BackColor actually works. The problem is that the label gets its BackColor from the form, since the form is its Parent. Therefore we must make the progress bar its parent and adapt its location as well, since now it must be specified relative to the progress bar. Add this code to your form:
Transparent BackColor 实际上有效。问题是标签从表单中获取它的 BackColor,因为表单是它的父级。因此,我们必须使进度条成为其父级并调整其位置,因为现在必须相对于进度条指定它。将此代码添加到您的表单中:
Public Sub New()
InitializeComponent()
Dim pos As Point = PointToScreen(lblPercentage.Location)
pos = myProgressBar.PointToClient(pos)
lblPercentage.Parent = myProgressBar
lblPercentage.Location = pos
lblPercentage.BackColor = Color.Transparent
End Sub
Alternatively you can calculate the location of the label like this
或者,您可以像这样计算标签的位置
lblPercentage.Location = New Point(lblPercentage.Location.X - myProgressBar.Location.X,
lblPercentage.Location.Y - myProgressBar.Location.Y)
You cannot make this in the designer, since your progress bar is probably not a container control (i.e. placing a label on it does not make it a child control of the bar) and you will not see the result in the designer.
您不能在设计器中进行此操作,因为您的进度条可能不是容器控件(即在其上放置标签不会使其成为该条的子控件)并且您不会在设计器中看到结果。
UPDATE
更新
You could also try these alternatives:
您也可以尝试以下替代方案:
- Draw the percentage number in the
OnPaintmethod of your control (overrideOnPaint). - Do what I showed you above inside the progress bar. You could add the label programmatically in the constructor of the progress bar.
- Use a
UserControlas base class of your progress bar. This would allow you to place the label on it in the designer.
- 在
OnPaint您的控制方法(覆盖OnPaint)中绘制百分比数字。 - 执行我上面在进度条中向您展示的操作。您可以在进度条的构造函数中以编程方式添加标签。
- 使用 a
UserControl作为进度条的基类。这将允许您将标签放置在设计器中。
回答by Ravindra Bagale
here you have made your progress bar transparent that means it transparent only to progress bar and behind the progress bar there is form, thats why it showing form.
It's a Windows restriction that transparency effects are relative to the top-level window, stacking effects do not work.always You will see the formas the background,
在这里,您已将进度条设为透明,这意味着它仅对进度条透明,并且在进度条后面有表单,这就是它显示表单的原因。透明效果是相对于顶层窗口而言是 Windows 的限制,堆叠效果不起作用。总是你会看到form作为背景,

