Skip to content

Instantly share code, notes, and snippets.

@facebookegypt
Created May 5, 2020 20:29
Show Gist options
  • Save facebookegypt/4afa66daa758d290590f61f8ee1647f3 to your computer and use it in GitHub Desktop.
Save facebookegypt/4afa66daa758d290590f61f8ee1647f3 to your computer and use it in GitHub Desktop.
Visual Basic Class - Custom Progress Bar With Text shows progress %
Imports System.ComponentModel
Public Class ProgressBarWithText
Inherits ProgressBar
'Note : ME=Control
Public Sub New()
'flags are used to categorize supported behavior. A control can enable a style by calling the SetStyle method
'and passing in the appropriate ControlStyles bit (or bits)
Me.SetStyle(ControlStyles.UserPaint, True)
Me.SetStyle(ControlStyles.OptimizedDoubleBuffer, True)
End Sub
'Specifies that a property or method is viewable in an editor.
<EditorBrowsable(EditorBrowsableState.Always), Browsable(True)>
Public Overrides Property Font() As Font
Get
Return MyBase.Font
End Get
Set(ByVal value As Font)
MyBase.Font = value
End Set
End Property
Protected Overrides Sub OnPaint(e As PaintEventArgs)
Dim g = e.Graphics
' value/area.
Dim rect As Rectangle = Me.ClientRectangle
Dim valueRange = ((Me.Value - Me.Minimum) / (Me.Maximum - Me.Minimum))
rect.Width = CInt(rect.Width * valueRange)
' draw bar.
ProgressBarRenderer.DrawHorizontalBar(g, Me.DisplayRectangle)
' fill bar value.
If rect.Width > 0 Then
Dim valuFillRect = rect : valuFillRect.Inflate(-1, -1)
ProgressBarRenderer.DrawHorizontalChunks(g, valuFillRect)
' Or use our own fill,
'Using lgb As New Drawing2D.LinearGradientBrush(rect, Color.FromArgb(120, Color.DarkBlue), _
'Color.FromArgb(120, Color.LightBlue), 180, False)
' g.FillRectangle(lgb, valuFillRect)
'End Using
End If
' draw text.
Dim percntText = valueRange.ToString("P0")
Using reg1 As New Region(rect)
Using reg2 As New Region(Me.ClientRectangle)
reg2.Exclude(reg1)
Dim textsize As SizeF = g.MeasureString(percntText, Me.Font)
Dim x As Single = (Me.ClientRectangle.Width - textsize.Width) / 2
Dim y As Single = (Me.ClientRectangle.Height - textsize.Height) / 2
g.Clip = reg1
g.DrawString(percntText, Me.Font, Brushes.White, x, y)
g.Clip = reg2
g.DrawString(percntText, Me.Font, Brushes.Black, x, y)
End Using
End Using
End Sub
Private Sub InitializeComponent()
'Temporarily suspends the layout logic for the control.
Me.SuspendLayout()
'Resumes the usual layout logic.
Me.ResumeLayout(False)
End Sub
End Class
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment