Custom ProgressBar

StewartBW 1,460 Reputation points
2025-04-27T21:40:51.6433333+00:00

Hi,

I need to make a custom ProgressBar which allows me to set the Vista 3 state colors in addition to orientation, in .NetFx 4.8 WinForms, wrote this code but orientation will not apply, confused, any tip please? :)

Public Class _MeterBar
    Inherits Windows.Forms.ProgressBar
    Private _Vertical As Boolean = False
    Private _Color As Color = Color.Green
    Public Sub New()
        MyBase.New()
        'Style = ProgressBarStyle.Continuous
    End Sub
    Protected Overrides ReadOnly Property CreateParams As CreateParams
        Get
            If _Vertical = False Then
                Dim MyCP As CreateParams = MyBase.CreateParams
                MyCP.Style = MyCP.Style
                Return MyCP
            Else
                Dim MyCP As CreateParams = MyBase.CreateParams
                MyCP.Style = MyCP.Style Or &H4
                Return MyCP
            End If
        End Get
    End Property
    Public Property ProgressVertical As Boolean
        Get
            Return _Vertical
        End Get
        Set(ByVal Vertical As Boolean)
            _Vertical = Vertical
            Invalidate()
        End Set
    End Property
    Public Property ProgressColor As Color
        Get
            Return _Color
        End Get
        Set(ByVal PRGColor As Color)
            Select Case PRGColor
                Case Color.Yellow
                    _Color = Color.Yellow
                    If InvokeRequired Then
                        Invoke(Sub() SendMessage(Me.Handle, &H400 + 16, CType(&H3, IntPtr), IntPtr.Zero))
                    Else
                        SendMessage(Me.Handle, &H400 + 16, CType(&H3, IntPtr), IntPtr.Zero)
                    End If
                    Invalidate()
                Case Color.Red
                    _Color = Color.Red
                    If InvokeRequired Then
                        Invoke(Sub() SendMessage(Me.Handle, &H400 + 16, CType(&H2, IntPtr), IntPtr.Zero))
                    Else
                        SendMessage(Me.Handle, &H400 + 16, CType(&H2, IntPtr), IntPtr.Zero)
                    End If
                    Invalidate()
                Case Else
                    _Color = Color.Green
                    If InvokeRequired Then
                        Invoke(Sub() SendMessage(Me.Handle, &H400 + 16, CType(&H1, IntPtr), IntPtr.Zero))
                    Else
                        SendMessage(Me.Handle, &H400 + 16, CType(&H1, IntPtr), IntPtr.Zero)
                    End If
                    Invalidate()
            End Select
        End Set
    End Property
End Class
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,840 questions
0 comments No comments
{count} votes

Accepted answer
  1. Castorix31 88,876 Reputation points
    2025-04-29T05:36:54.5066667+00:00

    I did some tests and apparently the control must be re-created (Me.RecreateHandle()) with PBS_VERTICAL style (although documentation says the reverse...)

    But there are also problems to redraw when state is PBST_ERROR or PBST_PAUSED (also when used directly in C++/Win32)

    So I think the best method is to use ProgressBarRenderer, like the sample in MSDN : https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.progressbarrenderer?view=windowsdesktop-9.0&redirectedfrom=MSDN

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.