What's New
Q & A
Tip Jar
C# Helper...
Follow VBHelper on Twitter
MSDN Visual Basic Community
TitleSlowly fill a circle with color from bottom to top in Visual Basic .NET
DescriptionThis example shows how to slowly fill a circle with color from bottom to top in Visual Basic .NET
Keywordscircle, ellipse, fill circle, fill ellipse, VB.NET, animation
CategoriesVB.NET, Graphics
The program draws an ellipse on its form. When the user clicks the ellipse, the program enables a timer. When the timer fires, it invalidates the form to geneate a Paint event.
' If the user clicked on the circle, start filling.
Private Sub Form1_MouseDown(ByVal sender As Object, ByVal e _
    As System.Windows.Forms.MouseEventArgs) Handles _
    Dim cx As Integer = m_CircleBounds.X + _
        m_CircleBounds.Width \ 2
    Dim cy As Integer = m_CircleBounds.Y + _
        m_CircleBounds.Height \ 2
    If (cx - e.X) * (cx - e.X) + (cy - e.Y) * (cy - e.Y) <= _
        100 * 100 Then
        tmrFiller.Enabled = True
    End If
End Sub

' Redraw.
Private Sub tmrFiller_Tick(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles tmrFiller.Tick
End Sub
The Paint event handler draws the circle in its background color. If the timer is enabled, it then colors the filled part. It creates a GraphicsPath object and adds an arc to it representing the filled area. It uses the Grpahics object's FillPath method to fill the area.

The code then increases the filled arc's sweep angle. If the angle is greater than 360 degrees, the whole circle is filled so the code disables the timer and resets for the next mouse click.

' Draw the circle.
Private Sub Form1_Paint(ByVal sender As Object, ByVal e As _
    System.Windows.Forms.PaintEventArgs) Handles Me.Paint
    ' Color the circle's background.
    Using br As New SolidBrush(m_BackColor)
        e.Graphics.FillEllipse(br, m_CircleBounds)
    End Using

    ' If the timer is enabled, fill part of the circle.
    If tmrFiller.Enabled Then
        ' Color the circle's filled part.
        Using br As New SolidBrush(m_FillColor), gp As New _
            Dim start_angle As Single = 90 - m_SweepAngle / _
            gp.AddArc(m_CircleBounds, start_angle, _
            e.Graphics.FillPath(br, gp)
            m_SweepAngle += 20
        End Using

        ' Increase the sweep angle.
        m_SweepAngle = CSng(m_SweepAngle + PI / 10)

        ' See if we're done with this color.
        If m_SweepAngle > 360 Then
            tmrFiller.Enabled = False
            m_BackColor = m_FillColor
            m_FillColorNumber = (m_FillColorNumber + 1) Mod _
            m_FillColor = m_Colors(m_FillColorNumber)
            m_SweepAngle = 0
        End If
    End If

    ' Outline the circle.
    e.Graphics.DrawEllipse(Pens.Black, m_CircleBounds)
End Sub
Copyright © 1997-2006 Rocky Mountain Computer Consulting, Inc.   All rights reserved.