如何将Label用作ProgressBar

时间:2016-06-17 14:32:04

标签: .net vb.net custom-controls

我对我正在做的项目有疑问。

我有一个包含8个标签的面板(标签1,标签2等)

我想要做的是将标签更改为带文本的进度条。 这是动态发生的。

注意:必须改回来。

我可以这样做吗?

此外,我必须在达到配额时将进度条颜色设置为红色和橙色。

我试过了:

Label1 = Progressbar1
Progressbar1.value = 75

但这不起作用 duuh

1 个答案:

答案 0 :(得分:3)

What I want to do is change a label into a progress bar with text.这样做,它看起来不像带有动画的标准进度条。要补偿这一点,将使用渐变:

Imports System.Drawing.Drawing2D

Public Class LabeledMeter
    Inherits Label

    Public Property MinValue As Int32 = 0
    Public Property MaxValue As Int32 = 100
    Private mVal As Int32 = 1
    Private mValue As Double
    Public Property Value As Int32
        Get
            Return mVal
        End Get
        Set(value As Int32)

            If mVal <= MaxValue AndAlso mVal >= MinValue Then
                mVal = value
                mValue = mVal / MaxValue
                Me.Invalidate()
            End If
        End Set
    End Property

    Public Property StartColor As Color = Color.LimeGreen
    Public Property EndColor As Color = Color.Firebrick

    Private meter As Boolean = False
    Public Property MeterDisplay As Boolean
        Get
            Return meter
        End Get
        Set(value As Boolean)
            If meter <> value Then
                meter = value
                Me.Invalidate()
            End If
        End Set
    End Property

    Public Sub New()
        MyBase.TextAlign = ContentAlignment.MiddleCenter
        MyBase.BorderStyle = Windows.Forms.BorderStyle.Fixed3D
    End Sub

    Protected Overrides Sub OnPaint(e As PaintEventArgs)
        If MeterDisplay = False Then
            MyBase.OnPaint(e)
            Return
        End If

        ' fun and games
        Dim c2 As Color = EndColor
        'If mValue < 0.51 Then
        '    c2 = Color.Yellow
        'End If

        Dim rect = New Rectangle(0, 0,
                        Convert.ToInt32(Width * mValue), Height)
        Using br As New LinearGradientBrush(rect, StartColor, c2, LinearGradientMode.Horizontal)
            e.Graphics.FillRectangle(br, rect)
        End Using

        ' could draw the Text - specs are not clear
        TextRenderer.DrawText(e.Graphics, mValue.ToString("P1"), MyBase.Font, ClientRectangle, MyBase.ForeColor)
    End Sub

    Protected Overrides Sub OnHandleCreated(e As EventArgs)
        MyBase.OnHandleCreated(e)
        MyBase.AutoSize = False
    End Sub

End Class

备注

  • MeterDisplay属性切换计量或仅标准标签模式。
  • Value更改已填写的百分比。仅当MeterDisplay为True时才重新绘制。
  • 文本与XX%测试显示可能是属性驱动的。
  • 降低开始颜色的alpha值有时可以提供良好的视觉效果

enter image description here

enter image description here