VB.NET 进度条百分比 [英] VB.NET Progress bar percentage

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

问题描述

我创建了一个完美运行的进度条.我最近添加了百分比,但我想在进度条顶部显示标签.

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.

像这样:

如您所见,唯一的问题是背景不透明.尽管有:

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?

推荐答案

透明背景色确实有效.问题是标签从表单中获取它的 BackColor,因为表单是它的父级.因此,我们必须使进度条成为其父级并调整其位置,因为现在必须相对于进度条指定它.将此代码添加到您的表单中:

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:

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.

更新

您也可以尝试以下替代方法:

You could also try these alternatives:

  • 在控件的OnPaint 方法中绘制百分比数字(覆盖OnPaint).
  • 执行我上面在进度条中向您展示的操作.您可以在进度条的构造函数中以编程方式添加标签.
  • 使用 UserControl 作为进度条的基类.这将允许您将标签放在设计器中.
  • Draw the percentage number in the OnPaint method of your control (override OnPaint).
  • 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 UserControl as base class of your progress bar. This would allow you to place the label on it in the designer.

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

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