INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Controls

Create a progress bar with text in VS 2008. by Sorwen
Posted: 20 Mar 09 (Edited 22 Jul 09)

I needed a simple progress bar with text.  When that didn't work I went ahead and created this. This cannot be used in VS 2005 because you cannot inherit from the progress bar.  MS locked that out.  You can however create a progress bar from scratch and then use that along with what is below.  A step-by-step for creating a smooth progress bar in 2k5 can be found here: http://support.microsoft.com/kb/323088

CODE

Public Class CustomProgressBar
    Inherits System.Windows.Forms.ProgressBar

    Enum BarTextStyle
        DefaultText = 0
        ValueText = 1
        ValueOfMax = 2
        PercentText = 3
    End Enum

    Enum BrushBarStyle
        Block = 0
        GradientHorizontal = 1
        GradientVertical = 2
        Solid = 3
        Picture = 4
    End Enum

    Private _BarStyle As BrushBarStyle
    Private _BackColor As System.Drawing.Color
    Private _Picture As System.Drawing.Image
    Private _TextStyle As BarTextStyle
    Private _Font As System.Drawing.Font
    Private _FontColor As System.Drawing.Color


    Sub New()
        MyBase.New()
        Me.SetStyle(Windows.Forms.ControlStyles.AllPaintingInWmPaint, True)
        Me.SetStyle(Windows.Forms.ControlStyles.UserPaint, True)
        Me.SetStyle(Windows.Forms.ControlStyles.OptimizedDoubleBuffer, True)
        Me.SetStyle(Windows.Forms.ControlStyles.ResizeRedraw, True)

        Me.BarStyle = BrushBarStyle.Solid
        Me.BackColor = System.Drawing.SystemColors.Control
        Me.ForeColor = System.Drawing.SystemColors.Highlight
        Me.Font = MyBase.Font
        Me.FontColor = System.Drawing.SystemColors.WindowText
    End Sub


#Region "Properties"
    ''' <summary>
    ''' Gets or sets the current progress.
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    <System.ComponentModel.Description("Sets the current progress.")> _
    Public Shadows Property Value() As Integer
        Get
            Return MyBase.Value
        End Get
        Set(ByVal value As Integer)
            If value < Me.Minimum Then
                value = Me.Minimum
            ElseIf value > Me.Maximum Then
                value = Me.Maximum
            End If

            MyBase.Value = value
            RaiseEvent ValueChanged(Me)
            Me.Invalidate()
        End Set
    End Property

    ''' <summary>
    ''' Gets or sets the style of the progress bar.
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    <System.ComponentModel.Description("Sets the style of the progress bar."), ComponentModel.DefaultValue(BrushBarStyle.Solid)> _
    Public Property BarStyle() As BrushBarStyle
        Get
            Return _BarStyle
        End Get
        Set(ByVal value As BrushBarStyle)
            _BarStyle = value
        End Set
    End Property

    ''' <summary>
    ''' Gets or sets the progress bar color.
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    <System.ComponentModel.Description("Sets the progress bar color"), ComponentModel.DefaultValue(GetType(System.Drawing.SystemColors), "Highlight")> _
    Public Overrides Property ForeColor() As System.Drawing.Color
        Get
            Return MyBase.ForeColor
        End Get
        Set(ByVal value As System.Drawing.Color)
            MyBase.ForeColor = value
        End Set
    End Property

    ''' <summary>
    ''' Gets or sets the back color of the progress bar.  This is used with Gradient style progress bars.
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    <System.ComponentModel.Description("Sets the back color of the progress bar.  This is used with Gradient style progress bars."), ComponentModel.DefaultValue(GetType(System.Drawing.SystemColors), "Control")> _
    Public Overrides Property BackColor() As System.Drawing.Color
        Get
            Return _BackColor
        End Get
        Set(ByVal value As System.Drawing.Color)
            _BackColor = value
        End Set
    End Property

    ''' <summary>
    ''' Gets or sets the color of the control.
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    <System.ComponentModel.Description("Sets the the color of the control."), ComponentModel.DefaultValue(GetType(System.Drawing.SystemColors), "Control")> _
    Public Property ControlColor() As System.Drawing.Color
        Get
            Return MyBase.BackColor
        End Get
        Set(ByVal value As System.Drawing.Color)
            MyBase.BackColor = value
        End Set
    End Property

    ''' <summary>
    ''' Gets or sets a picture that will be used instead of a color to show progress.
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    <System.ComponentModel.Description("Sets a picture that will be used instead of a color to show progress.")> _
    Public Property Picture() As System.Drawing.Image
        Get
            Return _Picture
        End Get
        Set(ByVal value As System.Drawing.Image)
            _Picture = value
        End Set
    End Property

    ''' <summary>
    ''' Gets or sets the text to be displayed when the TextStyle is set to DefaultText.
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    <System.ComponentModel.Description("Sets the text to be displayed when the TextStyle is set to DefaultText."), System.ComponentModel.Browsable(True)> _
    Public Overrides Property Text() As String
        Get
            Return MyBase.Text
        End Get
        Set(ByVal value As String)
            MyBase.Text = value
        End Set
    End Property

    ''' <summary>
    ''' Gets or sets the TextStyle used when outputting text.
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    <System.ComponentModel.Description("Sets the TextStyle used when outputting text."), ComponentModel.DefaultValue(BarTextStyle.DefaultText)> _
    Public Property TextStyle() As BarTextStyle
        Get
            Return _TextStyle
        End Get
        Set(ByVal value As BarTextStyle)
            _TextStyle = value
        End Set
    End Property

    ''' <summary>
    ''' Gets or sets the properties of the font used.
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    <System.ComponentModel.Browsable(True)> _
    Public Overrides Property Font() As System.Drawing.Font
        Get
            Return _Font
        End Get
        Set(ByVal value As System.Drawing.Font)
            _Font = value
        End Set
    End Property

    ''' <summary>
    ''' Gets or sets the font color for the text displayed.
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    <System.ComponentModel.Description("Sets the font color for the text displayed."), ComponentModel.DefaultValue(GetType(System.Drawing.SystemColors), "WindowText")> _
    Public Property FontColor() As System.Drawing.Color
        Get
            Return _FontColor
        End Get
        Set(ByVal value As System.Drawing.Color)
            _FontColor = value
        End Set
    End Property
#End Region

#Region "Events"
    Public Event ValueChanged(ByVal sender As Object)
#End Region

    Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
        e.Graphics.Clear(Me.ControlColor)

        If Me IsNot Nothing Then
            PaintBar(e.Graphics)
            PaintText(e.Graphics)
        End If

        MyBase.OnPaint(e)
    End Sub

    'Paint a bar graphic to the control
    Private Sub PaintBar(ByVal sender As System.Drawing.Graphics)
        'If text is painted then a bar must be painted as it will no longer show on its own.
        Dim percentage As Integer = (Me.Value / Me.Maximum * Me.ClientRectangle.Width)
        Dim rect As New System.Drawing.RectangleF(Me.ClientRectangle.X, Me.ClientRectangle.Y, percentage, Me.ClientRectangle.Height)
        Dim BarBrush As System.Drawing.Brush

        Select Case _BarStyle
            Case BrushBarStyle.Block
                BarBrush = New System.Drawing.Drawing2D.HatchBrush(Drawing.Drawing2D.HatchStyle.Vertical, Me.ControlColor, Me.ForeColor)
            Case BrushBarStyle.GradientHorizontal
                If percentage > 0 Then BarBrush = New System.Drawing.Drawing2D.LinearGradientBrush(rect, Me.ForeColor, Me.BackColor, Drawing.Drawing2D.LinearGradientMode.Horizontal)
            Case BrushBarStyle.GradientVertical
                If percentage > 0 Then BarBrush = New System.Drawing.Drawing2D.LinearGradientBrush(rect, Me.ForeColor, Me.BackColor, Drawing.Drawing2D.LinearGradientMode.Vertical)
            Case BrushBarStyle.Solid
                BarBrush = New System.Drawing.SolidBrush(Me.ForeColor)
            Case BrushBarStyle.Picture
                If _Picture IsNot Nothing Then BarBrush = New System.Drawing.TextureBrush(_Picture, Drawing.Drawing2D.WrapMode.Tile)
        End Select

        If BarBrush Is Nothing Then BarBrush = New System.Drawing.SolidBrush(Me.ForeColor)

        sender.SmoothingMode = Drawing.Drawing2D.SmoothingMode.HighSpeed
        sender.FillRectangle(BarBrush, rect)
        BarBrush.Dispose()
    End Sub

    'Paint text to the control
    Private Sub PaintText(ByVal sender As System.Drawing.Graphics)
        Dim x As Single
        Dim y As Single
        Dim text As String = " "
        Dim sizeF As System.Drawing.SizeF

        Select Case Me.TextStyle
            Case BarTextStyle.DefaultText
                text = Me.Text
            Case BarTextStyle.ValueText
                text = Me.Value
            Case BarTextStyle.ValueOfMax
                text = Me.Value & "/" & Me.Maximum & " " & Me.Font.Size & "/" & Me.Font.Size
            Case BarTextStyle.PercentText
                text = (Me.Value / Me.Maximum * 100)
        End Select

        sizeF = Me.CreateGraphics.MeasureString(text, Me.Font, Me.Width)
        x = (Me.Width / 2) - (sizeF.Width / 2)
        y = (Me.Height / 2) - (sizeF.Height / 2)

        sender.DrawString(text, Me.Font, New System.Drawing.SolidBrush(Me.FontColor), x, y)
    End Sub

End Class

The below code is an updated version of the above progress bar.  It now handles changing the text color as the progress bar crosses the text and allows the control to auto size the text to fit the size of the control.  This will also allow the progress bar to "overload" if the value exceeds the maximum rather than cause an exception.  The original progress bar color is shown, but a new progress bar is created with the overload color.

CODE

Public Class CustomProgressBar
    Inherits System.Windows.Forms.ProgressBar

    Enum BarTextStyle
        DefaultText = 0
        ValueText = 1
        ValueOfMax = 2
        PercentText = 3
    End Enum

    Enum BrushBarStyle
        Block = 0
        GradientHorizontal = 1
        GradientVertical = 2
        Solid = 3
        Picture = 4
    End Enum

    Private StartFontSize As Single
    Private _BarStyle As BrushBarStyle
    Private _GradientColor As System.Drawing.Color
    Private _Picture As System.Drawing.Image
    Private _TextStyle As BarTextStyle
    Private _Font As System.Drawing.Font
    Private _FontColor As System.Drawing.Color
    Private _FontHighlightColor As System.Drawing.Color
    Private _AllowFontHighlight As Boolean
    Private _FontAutoAdjust As Boolean
    Private _ActualValue As Integer

    Sub New()
        MyBase.New()
        Me.SetStyle(Windows.Forms.ControlStyles.AllPaintingInWmPaint, True)
        Me.SetStyle(Windows.Forms.ControlStyles.UserPaint, True)
        Me.SetStyle(Windows.Forms.ControlStyles.OptimizedDoubleBuffer, True)
        Me.SetStyle(Windows.Forms.ControlStyles.ResizeRedraw, True)

        Me.ForeColor = System.Drawing.SystemColors.Highlight
        Me.StartFontSize = MyBase.Font.Size
        Me.FontAutoSize = True
        _BarStyle = BrushBarStyle.Solid
        _Font = MyBase.Font
        _FontColor = System.Drawing.SystemColors.WindowText
        _FontHighlightColor = System.Drawing.SystemColors.HighlightText
        _GradientColor = System.Drawing.SystemColors.GradientActiveCaption
        _TextStyle = BarTextStyle.DefaultText
        _AllowFontHighlight = True
    End Sub


#Region "Properties"
    ''' <summary>
    ''' Gets or sets the current progress.
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    <System.ComponentModel.Description("Sets the current progress.")> _
    Public Shadows Property Value() As Integer
        Get
            Return MyBase.Value
        End Get
        Set(ByVal value As Integer)
            Dim cValue As Integer = value

            If cValue < Me.Minimum Then
                cValue = Me.Minimum
            ElseIf cValue > Me.Maximum Then
                Dim OrderOfMagnatude As Integer = Math.Floor(cValue / Me.Maximum)
                cValue = cValue - (Me.Maximum * OrderOfMagnatude)
            End If

            MyBase.Value = cValue
            _ActualValue = value
            RaiseEvent ValueChanged(Me)
            Me.Invalidate()
        End Set
    End Property

    ''' <summary>
    ''' Gets or sets the style of the progress bar.
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    <System.ComponentModel.Description("Sets the style of the progress bar."), ComponentModel.DefaultValue(BrushBarStyle.Solid)> _
    Public Property BarStyle() As BrushBarStyle
        Get
            Return _BarStyle
        End Get
        Set(ByVal value As BrushBarStyle)
            _BarStyle = value
        End Set
    End Property

    ''' <summary>
    ''' Gets or sets the progress bar color.
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    <System.ComponentModel.Description("Sets the progress bar color"), ComponentModel.DefaultValue(GetType(System.Drawing.SystemColors), "Highlight")> _
    Public Overrides Property ForeColor() As System.Drawing.Color
        Get
            Return MyBase.ForeColor
        End Get
        Set(ByVal value As System.Drawing.Color)
            MyBase.ForeColor = value
        End Set
    End Property

    ''' <summary>
    ''' Gets or sets the back color of the progress bar.  This is used with Gradient style progress bars.
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    <System.ComponentModel.Description("Sets the back color of the progress bar.  This is used with Gradient style progress bars."), ComponentModel.DefaultValue(GetType(System.Drawing.SystemColors), "Control"), ComponentModel.Browsable(True)> _
    Public Property GradientColor() As System.Drawing.Color
        Get
            Return _GradientColor
        End Get
        Set(ByVal value As System.Drawing.Color)
            _GradientColor = value
        End Set
    End Property

    ''' <summary>
    ''' Gets or sets a picture that will be used instead of a color to show progress.
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    <System.ComponentModel.Description("Sets a picture that will be used instead of a color to show progress.")> _
    Public Property Picture() As System.Drawing.Image
        Get
            Return _Picture
        End Get
        Set(ByVal value As System.Drawing.Image)
            _Picture = value
        End Set
    End Property

    ''' <summary>
    ''' Gets or sets the text to be displayed when the TextStyle is set to DefaultText.
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    <System.ComponentModel.Description("Sets the text to be displayed when the TextStyle is set to DefaultText."), System.ComponentModel.Browsable(True)> _
    Public Overrides Property Text() As String
        Get
            Return MyBase.Text
        End Get
        Set(ByVal value As String)
            MyBase.Text = value
        End Set
    End Property

    ''' <summary>
    ''' Gets or sets the TextStyle used when outputting text.
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    <System.ComponentModel.Description("Sets the TextStyle used when outputting text."), ComponentModel.DefaultValue(BarTextStyle.DefaultText)> _
    Public Property TextStyle() As BarTextStyle
        Get
            Return _TextStyle
        End Get
        Set(ByVal value As BarTextStyle)
            _TextStyle = value

            If Me.Visible = True Then Me.Invalidate()
        End Set
    End Property

    ''' <summary>
    ''' Gets or sets the properties of the font used.
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    <System.ComponentModel.Browsable(True)> _
    Public Overrides Property Font() As System.Drawing.Font
        Get
            Return _Font
        End Get
        Set(ByVal value As System.Drawing.Font)
            _Font = value
        End Set
    End Property

    ''' <summary>
    ''' Gets or sets the font color for the text displayed.
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    <System.ComponentModel.Description("Sets the font color for the text displayed."), ComponentModel.DefaultValue(GetType(System.Drawing.SystemColors), "WindowText")> _
    Public Property FontColor() As System.Drawing.Color
        Get
            Return _FontColor
        End Get
        Set(ByVal value As System.Drawing.Color)
            _FontColor = value
        End Set
    End Property

    ''' <summary>
    ''' Gets or sets the font highlight color used to display progress filled text.
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    <System.ComponentModel.Description("Sets the font color for the text displayed."), ComponentModel.DefaultValue(GetType(System.Drawing.SystemColors), "HighlightText")> _
    Public Property FontHighlightcolor() As System.Drawing.Color
        Get
            Return _FontHighlightColor
        End Get
        Set(ByVal value As System.Drawing.Color)
            _FontHighlightColor = value
        End Set
    End Property

    ''' <summary>
    ''' Gets or sets if the text should highlight as the progress bar crosses the text.
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    <System.ComponentModel.Description("Sets if the text should highlight as the progress bar crosses the text."), ComponentModel.DefaultValue(True)> _
    Public Property AllowFontHighlight() As Boolean
        Get
            Return _AllowFontHighlight
        End Get
        Set(ByVal value As Boolean)
            _AllowFontHighlight = value
        End Set
    End Property

    ''' <summary>
    ''' Gets or sets if the size of the font should adjust depending on the size of the control.
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    <System.ComponentModel.Description("Sets if the size of the font should adjust depending on the size of the control."), ComponentModel.DefaultValue(True)> _
    Public Property FontAutoSize() As Boolean
        Get
            Return _FontAutoAdjust
        End Get
        Set(ByVal value As Boolean)
            _FontAutoAdjust = value
        End Set
    End Property
#End Region

#Region "Events"
    Public Event ValueChanged(ByVal sender As Object)
#End Region

    Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
        e.Graphics.Clear(Me.BackColor)

        If Me IsNot Nothing Then
            PaintBar(e.Graphics)
            PaintText(e.Graphics)
        End If

        MyBase.OnPaint(e)
    End Sub

    'Paint a bar graphic to the control
    Private Sub PaintBar(ByVal sender As System.Drawing.Graphics)
        'If text is painted then a bar must be painted as it will no longer show on its own.
        Dim percentage As Double = (Me.Value / Me.Maximum * Me.ClientRectangle.Width)
        Dim rect As New System.Drawing.RectangleF(Me.ClientRectangle.X, Me.ClientRectangle.Y, percentage, Me.ClientRectangle.Height)
        Dim BarBrush As System.Drawing.Brush
        Dim cBackColor As System.Drawing.Color = Me.BackColor
        Dim cForeColor As System.Drawing.Color = Me.ForeColor

        Select Case _BarStyle
            Case BrushBarStyle.Block
                BarBrush = New System.Drawing.Drawing2D.HatchBrush(Drawing.Drawing2D.HatchStyle.Vertical, cBackColor, Me.ForeColor)
            Case BrushBarStyle.GradientHorizontal
                If percentage > 0 Then BarBrush = New System.Drawing.Drawing2D.LinearGradientBrush(rect, cForeColor, _GradientColor, Drawing.Drawing2D.LinearGradientMode.Horizontal)
            Case BrushBarStyle.GradientVertical
                If percentage > 0 Then BarBrush = New System.Drawing.Drawing2D.LinearGradientBrush(rect, cForeColor, _GradientColor, Drawing.Drawing2D.LinearGradientMode.Vertical)
            Case BrushBarStyle.Solid
                BarBrush = New System.Drawing.SolidBrush(cForeColor)
            Case BrushBarStyle.Picture
                If _Picture IsNot Nothing Then BarBrush = New System.Drawing.TextureBrush(_Picture, Drawing.Drawing2D.WrapMode.Tile)
        End Select

        If BarBrush Is Nothing Then BarBrush = New System.Drawing.SolidBrush(cForeColor)

        If _ActualValue > Me.Maximum Then
            sender.SmoothingMode = Drawing.Drawing2D.SmoothingMode.Default
            sender.FillRectangle(BarBrush, Me.ClientRectangle)

            Dim ErrorColor As System.Drawing.Color
            Dim Magnatude As Integer = Math.Floor(_ActualValue / Me.Maximum) * 50
            Dim RevMag As Integer = Magnatude

            If Magnatude >= 250 Then
                Magnatude = 0
                RevMag = 255
            End If

            If Me.ForeColor = Drawing.Color.Red Then
                ErrorColor = Drawing.Color.FromArgb(RevMag, 0, Magnatude, 0)
            Else
                ErrorColor = Drawing.Color.FromArgb(RevMag, Magnatude, 0, 0)
            End If

            BarBrush = New System.Drawing.SolidBrush(ErrorColor)
        End If


        sender.SmoothingMode = Drawing.Drawing2D.SmoothingMode.Default
        sender.FillRectangle(BarBrush, rect)
        BarBrush.Dispose()
    End Sub

    Private Sub PaintText(ByVal sender As System.Drawing.Graphics)
        Dim percentage As Double = (Me.Value / Me.Maximum * Me.ClientRectangle.Width)
        Dim cText As String = " "
        Dim FsizeF As System.Drawing.SizeF
        Dim ActualSizeF As System.Drawing.SizeF
        Dim NeededSizeF As System.Drawing.SizeF
        Dim strFmt As New System.Drawing.StringFormat
        Dim padNum As Integer
        Dim TextBrush As System.Drawing.Brush
        Dim HighlightBrush As System.Drawing.Brush

        strFmt.LineAlignment = Drawing.StringAlignment.Near
        strFmt.Alignment = Drawing.StringAlignment.Center
        strFmt.Trimming = Drawing.StringTrimming.None

        Select Case _TextStyle
            Case BarTextStyle.DefaultText
                cText = Me.Text
                padNum = 0
            Case BarTextStyle.ValueText
                cText = Me.Value
                padNum = Maximum.ToString.Length
            Case BarTextStyle.ValueOfMax
                cText = Me.Value & "/" & Me.Maximum
                padNum = (Me.Maximum.ToString.Length * 2) + 1
            Case BarTextStyle.PercentText
                cText = Math.Floor(Me.Value / Me.Maximum * 100) & "%"
                padNum = 4
            Case Else
                cText = Me.Text
                padNum = 0
        End Select

        FsizeF = Me.CreateGraphics.MeasureString(cText, _Font, Me.ClientSize.Width)

        If Me.FontAutoSize = True Then
            If Not Math.Floor(FsizeF.ToSize.Height) <= Me.ClientRectangle.Height Or Math.Floor(FsizeF.ToSize.Height) < Me.ClientRectangle.Height - 1 Then
                FsizeF = FontAdjust(cText, FsizeF)
            End If
        End If

        ActualSizeF = Me.CreateGraphics.MeasureString(cText, _Font, New System.Drawing.SizeF(Me.ClientSize), strFmt)
        NeededSizeF = Me.CreateGraphics.MeasureString(cText.PadLeft(padNum, "0"), _Font, New System.Drawing.SizeF(Me.ClientSize), strFmt)
        Dim rect As New System.Drawing.RectangleF((NeededSizeF.ToSize.Width - ActualSizeF.ToSize.Width), Me.ClientRectangle.Y, Me.ClientRectangle.Width - (NeededSizeF.ToSize.Width - ActualSizeF.ToSize.Width), Me.ClientRectangle.Height - 1)

        Dim HighlightGradient1 As Drawing.Color = _FontColor
        Dim HighlightGradient2 As Drawing.Color = _FontHighlightColor

        Select Case _BarStyle
            Case BrushBarStyle.GradientHorizontal
                Dim TextGradRect As New System.Drawing.RectangleF((rect.Width / 2) + rect.X, rect.Y, rect.Width / 2, rect.Height)

                TextBrush = New System.Drawing.Drawing2D.LinearGradientBrush(rect, Me.ForeColor, _GradientColor, Drawing.Drawing2D.LinearGradientMode.Horizontal)
                HighlightBrush = New System.Drawing.Drawing2D.LinearGradientBrush(TextGradRect, HighlightGradient1, HighlightGradient2, Drawing.Drawing2D.LinearGradientMode.Horizontal)
            Case BrushBarStyle.GradientVertical
                Dim TextGradRect As New System.Drawing.RectangleF((rect.Width / 2) + rect.X, rect.Y, rect.Width / 2, rect.Height)

                TextBrush = New System.Drawing.Drawing2D.LinearGradientBrush(rect, Me.ForeColor, _GradientColor, Drawing.Drawing2D.LinearGradientMode.Vertical)
                HighlightBrush = New System.Drawing.Drawing2D.LinearGradientBrush(TextGradRect, HighlightGradient1, HighlightGradient2, Drawing.Drawing2D.LinearGradientMode.Vertical)
            Case Else
                TextBrush = New System.Drawing.SolidBrush(_FontColor)
                HighlightBrush = New System.Drawing.SolidBrush(_FontHighlightColor)
        End Select

        sender.DrawString(cText, _Font, TextBrush, rect, strFmt)

        If Me.AllowFontHighlight = True Then
            sender.Clip = New Drawing.Region(New System.Drawing.RectangleF(percentage - Me.ClientRectangle.Width, Me.ClientRectangle.Y, Me.ClientRectangle.Width, Me.ClientRectangle.Height))
            sender.DrawString(cText, _Font, HighlightBrush, rect, strFmt)
        End If
    End Sub

    Private Function FontAdjust(ByVal ptext As String, ByVal Size As System.Drawing.SizeF) As System.Drawing.SizeF
        Dim sizeF As System.Drawing.SizeF = Me.CreateGraphics.MeasureString(Text, _Font, Me.ClientRectangle.Width)

        If ptext <> "" Then
            If Not Math.Floor(sizeF.ToSize.Height) <= Me.ClientRectangle.Height Then
                If Me.ClientRectangle.Height > 2 Then
                    sizeF = New System.Drawing.SizeF(sizeF.Width, Me.ClientRectangle.Height)

                    Do Until Math.Floor(Size.Height) <= Math.Floor(sizeF.Height)
                        _Font = New System.Drawing.Font(_Font.FontFamily, _Font.Size - 0.1, _Font.Style, Drawing.GraphicsUnit.Pixel)
                        Size = Me.CreateGraphics.MeasureString(ptext, _Font, Me.ClientSize.Width)
                    Loop

                    Return sizeF
                End If
            ElseIf Math.Floor(sizeF.ToSize.Height) < Me.ClientRectangle.Height - 1 Then
                If Me.ClientRectangle.Height > 2 Then
                    sizeF = New System.Drawing.SizeF(sizeF.Width, Me.ClientRectangle.Height)

                    Do Until Math.Floor(Size.Height) >= Math.Floor(sizeF.Height - 1)
                        _Font = New System.Drawing.Font(_Font.FontFamily, _Font.Size + 0.1, _Font.Style, Drawing.GraphicsUnit.Pixel)
                        Size = Me.CreateGraphics.MeasureString(ptext, _Font, Me.ClientSize.Width)
                    Loop

                    Return sizeF
                End If
            End If
        End If

        Return sizeF
    End Function

End Class

Back to Visual Basic (Microsoft) VB.NET FAQ Index
Back to Visual Basic (Microsoft) VB.NET Forum

My Archive

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close