none
DrawProgress question (Need help soon as possible).

    Question

  • I found this awesome code online [HERE] to make a circle progress bar. I understand the code somewhat and understand how to change colors/progress values, but I don't under how to keep the value updated? So to changed the value of the drawn progress bar you have to change the value 40 in this line of code: (Also I couldn't get BR1COP code to work? It changes the value, but doesn't work for me).

    DrawProgress(e.Graphics, New Rectangle(5, 5, 60, 60), 40)


    So now I'm having troubles keeping it updated (I'm not sure what else to call it other than updated). I want to change the value with "ProgressBar1.Value", it works like that but only when the form first opens.. I want the drawn progress bar to match visual basics ProgressBar1. So when vb's ProgressBar1 value changes the drawn one will too (40). Please help soon as possible. I've been search way to do this and trying many ways.  Here is the full code if anyone needs it.

     accepted
    	
    
    How about just drawing your own using GDI+.
    
    You can convert this to your own usercontrol later but this will get you started. It should be fairly self explanatory:
    
    Private Sub Form2_Paint(sender As Object, e As PaintEventArgs) Handles Me.Paint
        DrawProgress(e.Graphics, New Rectangle(5, 5, 60, 60), 40)
        DrawProgress(e.Graphics, New Rectangle(80, 5, 60, 60), 80)
        DrawProgress(e.Graphics, New Rectangle(155, 5, 60, 60), 57)
    End Sub
    
    Private Sub DrawProgress(g As Graphics, rect As Rectangle, percentage As Single)
        'work out the angles for each arc
        Dim progressAngle = CSng(360 / 100 * percentage)
        Dim remainderAngle = 360 - progressAngle
    
        'create pens to use for the arcs
        Using progressPen As New Pen(Color.LightSeaGreen, 2), remainderPen As New Pen(Color.LightGray, 2)
            'set the smoothing to high quality for better output
            g.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias
            'draw the blue and white arcs
            g.DrawArc(progressPen, rect, -90, progressAngle)
            g.DrawArc(remainderPen, rect, progressAngle - 90, remainderAngle)
        End Using
    
        'draw the text in the centre by working out how big it is and adjusting the co-ordinates accordingly
        Using fnt As New Font(Me.Font.FontFamily, 14)
            Dim text As String = percentage.ToString + "%"
            Dim textSize = g.MeasureString(text, fnt)
            Dim textPoint As New Point(CInt(rect.Left + (rect.Width / 2) - (textSize.Width / 2)), CInt(rect.Top + (rect.Height / 2) - (textSize.Height / 2)))
            'now we have all the values draw the text
            g.DrawString(text, fnt, Brushes.Black, textPoint)
        End Using
    End Sub

    This is what the drawn progress bar looks like:

    Friday, April 21, 2017 11:48 PM

All replies

  • So now I'm having troubles keeping it updated (I'm not sure what else to call it other than updated).

    The code that does the work is
        DrawProgress(e.Graphics, New Rectangle(5, 5, 60, 60), 40)

    so if you replace '40' with the variable that contains the value that you want to draw, and call Me.Refresh, then the progress bar will be re-drawn with that value.   (I presume you would only want one DrawProgress in the Paint event).

    Dim UpdateValue as Integer = 0 ... UpdateValue = 55 Me.Refresh ...

    Private Sub Form2_Paint(sender As Object, e As PaintEventArgs) Handles Me.Paint
        DrawProgress
    (e.Graphics, New Rectangle(5, 5, 60, 60), UpdateValue)
    End Sub

    How or when you change UpdateValue depends on what the progress circle is showing the progress of.

    Saturday, April 22, 2017 12:52 AM
  • Awesome, worked great. Thank you very much. How is there a way to hide to drawn progress bar with code? So I have have like a button and once it's clicked it will hide the drawn one?
    Saturday, April 22, 2017 2:06 AM
  • How is there a way to hide to drawn progress bar with code?

    Create a form-level variable to indicate whether the progress bar is active or not.  Set our unset the variable as required.

    Dim ShowProgress As Boolean = False
    ...
    ShowProgress = True
    ...
    
    Private Sub Form2_Paint(sender As Object, e As PaintEventArgs) Handles Me.Paint
        If ShowProgress then DrawProgress(e.Graphics, New Rectangle(5, 5, 60, 60), UpdateValue)
    End Sub

    You should edit the thread title to remove the urgency.

    Saturday, April 22, 2017 2:19 AM