Dear sir
My progress bar is not full fill (end area)and precentage is not center how can do it.please follow my attachment
thank you
wansa

Public Class Form1
    Declare Auto Function SendMessage Lib "user32.dll" (ByVal hwnd As IntPtr, ByVal msg As Integer, ByVal wparam As Integer, ByVal iparam As Integer) As Integer

    Enum ProgressBarcolor
        red = &H2
        green = &H1
        Yellow = &H3
    End Enum

    Private Shared Sub ChangeprogBarcolor(ByVal progressbar_name As Windows.Forms.ProgressBar, ByVal progressbar_color As ProgressBarcolor)
        SendMessage(progressbar_name.Handle, &H410, progressbar_color, 0)
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        SendMessage(ProgressBar1.Handle, 1040, 2, 0)
        Timer1.Start()
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        ProgressBar1.Value += 1
        Me.ProgressBar1.Refresh()
        UpdatePBar()
        If ProgressBar1.Value = 100 Then
            Timer1.Stop()
        End If
    End Sub

    Private Sub UpdatePBar()
        Dim x As Single
        Dim y As Single
        Dim percentage As String = CType((ProgressBar1.Value / ProgressBar1.Maximum * 100), Integer).ToString & "%"
        Dim gr As Graphics = Me.ProgressBar1.CreateGraphics
        Dim sz As SizeF = gr.MeasureString(percentage, Me.ProgressBar1.Font, Me.ProgressBar1.Width)
        x = (Me.ProgressBar1.Width / 2) - (sz.Width / 2)
        y = (Me.ProgressBar1.Height / 2) - (sz.Height / 2)
        If ProgressBar1.Value >= 50 Then
            gr.DrawString(percentage, New Font("Century Gothic", "11"), Brushes.LightGreen, x, y)

        Else
            gr.DrawString(percentage, New Font("Century Gothic", "11"), Brushes.Black, x, y)
        End If
    End Sub
End Class

Recommended Answers

All 3 Replies

As far as I can tell the SendMessage function that changes the state (and thus the colour) of your ProgressBar is what causes the bar to not fully display. When I commented it out the bar fully filled but, of course, was no longer red "error".

As for centering your percentage it seems to be caused by the font used when you first created ProgressBar, so to make sure you calculate the MesureString you should make sure to use the same font as when you draw the percentage over the bar like so:

Dim sz As SizeF = gr.MeasureString(percentage, New Font("Century Gothic", "11"), ProgressBar1.Width)

Or to put the currect font, font weight and font size in the designer view of your form.

I find no way, unless having a class inherited from ProgressBar:

Public Class Form1
    Dim myPBar As New MyProgressBar
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        With Me.ProgressBar1
            .Visible = False
            myPBar.Location = .Location
            myPBar.Minimum = .Minimum
            myPBar.Maximum = .Maximum
            myPBar.Size = .Size
            myPBar.Step = .Step
            myPBar.Style = .Style
            myPBar.BackColor = .BackColor
            myPBar.ForeColor = .ForeColor

            myPBar.Visible = Visible
            Me.Controls.Remove(Me.ProgressBar1)
            Me.Controls.Add(myPBar)
            .Dispose()
        End With
        Timer1.Start()
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        myPBar.Value += 1
        myPBar.Refresh()
        If myPBar.Value = 100 Then
            Timer1.Stop()
        End If
    End Sub

    Public Class MyProgressBar
        Inherits System.Windows.Forms.ProgressBar
        Declare Auto Function SendMessage Lib "user32.dll" (ByVal hwnd As IntPtr, ByVal msg As Integer, ByVal wparam As Integer, ByVal iparam As Integer) As Integer
        Public Sub New()
            MyBase.New()
            SetStyle(ControlStyles.UserPaint, True)
            SendMessage(Handle, 1040, 2, 0)
        End Sub
        Protected Overrides Sub OnPaint(e As System.Windows.Forms.PaintEventArgs)
            Try
                Dim gr As Graphics = e.Graphics
                Dim rec As Rectangle = e.ClipRectangle
                rec.Width = (rec.Width * (Value / Maximum)) - 4
                If (ProgressBarRenderer.IsSupported) Then
                    ProgressBarRenderer.DrawHorizontalBar(e.Graphics, e.ClipRectangle)
                End If
                rec.Height = rec.Height - 4
                e.Graphics.FillRectangle(Brushes.Red, 2, 2, rec.Width, rec.Height)
                Dim x As Single
                Dim y As Single
                Dim percentage As String = CType((Value * 100 / Maximum), Integer).ToString & "%"
                Dim sz As SizeF = gr.MeasureString(percentage, Font, Width)
                x = (Width / 2) - (sz.Width / 2)
                y = (Height / 2) - (sz.Height / 2) - 2
                If Value >= 50 Then
                    gr.DrawString(percentage, New Font("Century Gothic", "11"), Brushes.LightGreen, x, y)
                Else
                    gr.DrawString(percentage, New Font("Century Gothic", "11"), Brushes.Black, x, y)
                End If
            Catch ex As Exception

            End Try
        End Sub
    End Class

End Class

There is a collection of progress bars out there. One approximation to Click Here and yours:

Imports System.Drawing.Drawing2D

Public Class Form1
    Dim myPBar As New MyProgressBar
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        With Me.ProgressBar1
            .Visible = False
            myPBar.Location = .Location
            myPBar.Minimum = .Minimum
            myPBar.Maximum = .Maximum
            myPBar.Size = .Size
            myPBar.Step = .Step
            myPBar.Style = .Style
            myPBar.BackColor = .BackColor
            myPBar.ForeColor = .ForeColor

            myPBar.Visible = Visible
            Me.Controls.Remove(Me.ProgressBar1)
            Me.Controls.Add(myPBar)
            .Dispose()
        End With
        Timer1.Start()
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        If myPBar.Value = 100 Then
            myPBar.Value = 0
        End If
        myPBar.Value += 1
        myPBar.Refresh()
        If myPBar.Value = 100 Then
            Timer1.Stop()
        End If
    End Sub

    Public Class MyProgressBar
        Inherits System.Windows.Forms.ProgressBar
        Dim bBlend As New Blend
        Dim GradiantPosition As GradiantArea
        Dim BorderPen As Pen
        Dim _GradiantColor As Color = Color.White
        Dim _ProgressColor As Color = Color.Red
        Public Enum GradiantArea As Integer
            None = 0
            Top = 1
            Center = 2
            Bottom = 3
        End Enum
        Public Sub New()
            MyBase.New()
            SetStyle(ControlStyles.UserPaint Or _
                    ControlStyles.OptimizedDoubleBuffer Or _
                    ControlStyles.SupportsTransparentBackColor, True)
            MyBase.TabStop = False
            bBlend.Positions = New Single() {0.0F, 0.2F, 0.4F, 0.6F, 0.9F, 1.0F}
            GradiantPosition = GradiantArea.Top
            MyBase.BackColor = Color.Transparent
            BorderPen = New Pen(Color.Black)
        End Sub

        Protected Overrides Sub OnPaint(e As System.Windows.Forms.PaintEventArgs)
            Try
                e.Graphics.SmoothingMode = SmoothingMode.AntiAlias
                Dim gr As Graphics = e.Graphics
                Dim rec As Rectangle = e.ClipRectangle
                Dim rad As Integer = CInt(rec.Height / 2.5)
                Dim StartPoint As New Point(0, 0)
                Dim EndPoint As New Point(0, Me.Height)
                Using gp As New GraphicsPath
                    MakePath(gp, rec, rad)
                    Using _BackColorBrush As New LinearGradientBrush( _
                    StartPoint, EndPoint, BackColor, _GradiantColor)
                        _BackColorBrush.Blend = bBlend
                        MakePath(gp, rec, rad)
                        e.Graphics.FillPath(_BackColorBrush, gp)
                    End Using
                    rec.Width = (rec.Width * (Value / Maximum))
                    rec.Height = rec.Height - 4
                    Using _ProgressBrush As New LinearGradientBrush(StartPoint, EndPoint, _
                                        _ProgressColor, _GradiantColor)
                        _ProgressBrush.Blend = bBlend
                        rec.Height -= 1
                        rec.Width -= 1
                        Using gp2 As New GraphicsPath
                            MakePath(gp2, rec, rad)
                            Using gp3 As New GraphicsPath
                                Using rgn As New Region(gp)
                                    rgn.Intersect(gp2)
                                    gp3.AddRectangles(rgn.GetRegionScans(New Matrix))
                                End Using
                                e.Graphics.FillPath(_ProgressBrush, gp3)
                            End Using
                        End Using
                    End Using
                    Dim x As Single
                    Dim y As Single
                    Dim percentage As String = CType((Value * 100 / Maximum), Integer).ToString & "%"
                    Dim sz As SizeF = gr.MeasureString(percentage, Font, Width)
                    x = (Width - sz.Width) / 2
                    y = (Height - sz.Height) / 2 - 4
                    If Value >= 50 Then
                        gr.DrawString(percentage, New Font("Century Gothic", "11"), Brushes.Black, x + 1, y + 1)
                        gr.DrawString(percentage, New Font("Century Gothic", "11"), Brushes.LightGreen, x, y)
                    Else
                        gr.DrawString(percentage, New Font("Century Gothic", "11"), Brushes.LightGreen, x + 1, y + 1)
                        gr.DrawString(percentage, New Font("Century Gothic", "11"), Brushes.Black, x, y)
                    End If
                    rec = New Rectangle(0, 0, Me.Width - 1, Me.Height - 1)
                    MakePath(gp, rec, rad)
                    e.Graphics.DrawPath(Pens.DarkGray, gp)
                End Using

            Catch ex As Exception

            End Try
        End Sub
        Private Sub MakePath(ByRef pth As GraphicsPath, ByVal rc As Rectangle, ByVal rad As Integer)
            pth.Reset()
            pth.AddArc(rc.X, rc.Y, rad, rad, 180, 90)
            pth.AddArc(rc.Right - (rad), rc.Y, rad, rad, 270, 90)
            pth.AddArc(rc.Right - (rad), rc.Bottom - (rad), rad, rad, 0, 90)
            pth.AddArc(rc.X, rc.Bottom - (rad), rad, rad, 90, 90)
            pth.CloseFigure()
        End Sub

    End Class

End Class
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.