Simple Progress Indicator Control

Introduction

This a simple circular  progress indicator control, hope it will help somebody

 The Progress Indicator Control Code


Imports System.Windows.Forms
Imports System.Drawing
Imports System.Drawing.Drawing2D

Namespace RiverNile

    '****************************************************************************************'
    ' Module Name:  RiverNile.Indicator
    ' Project:      ProgressIndicatorControl
    ' Copyright (c) RiverNile.Net.
    ' All other rights reserved. silverlight1212@yahoo.com
    ' The sample demonstrates how to buidl a simple progress indicator using backbufferd image
    '
    '
    ' This source is subject to the Public License.
    ' Follow my blog for future enhancement
    '
    '
    ' THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER
    ' EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
    ' MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
    '****************************************************************************************'
    Public Class Indicator
        Inherits Control

        Private _value As Long
        Private _maximum As Long = 100

        Public Sub New()
            Me.Size = New Size(100, 100)
        End Sub

        Public Property Value() As Long
            Get
                Return _value
            End Get
            Set(value As Long)
                If value > _maximum Then value = _maximum
                _value = value
                Invalidate()
            End Set
        End Property

        Public Property Maximum As Long
            Get
                Return _maximum
            End Get
            Set(value As Long)
                If value < 1 Then value = 1
                _maximum = value
                Invalidate()
            End Set
        End Property

        '''
        '''Not Applicaple, as buffered image will be handled by the code.
        '''

        '''
        '''
        Protected Overrides Sub OnPaintBackground(pevent As PaintEventArgs)
            'MyBase.OnPaintBackground(pevent)
        End Sub

        Protected Overrides Sub OnPaint(e As PaintEventArgs)
            MyBase.OnPaint(e)

            BufferDrawing.DrawProgressIndicator(e.Graphics, Me.Width, Me.Height, ControlPaint.Dark(Me.BackColor), Me.BackColor, 2, Me._maximum, Me._value, Me.Font)
        End Sub

    End Class ' Indicator

    Public NotInheritable Class BufferDrawing

        Friend Shared Sub DrawProgressIndicator(gr As Graphics, imgWidth As Integer, _
                                     imgHeight As Integer, indicatorColor As Color, _
                                     indicatorBackcolor As Color, thick As Integer, _
                                     maxValue As Long, indicatorValue As Long, textFont As Font)


            Using bmp As New Bitmap(imgWidth, imgHeight)
                Using g As Graphics = Graphics.FromImage(bmp)
                    g.SmoothingMode = SmoothingMode.AntiAlias
                    g.SmoothingMode = SmoothingMode.HighQuality
                    g.Clear(indicatorBackcolor)

                    Using sb As New SolidBrush(indicatorColor)
                        Using indicatorPen As New Pen(sb, thick)
                            indicatorPen.StartCap = LineCap.Round
                            indicatorPen.EndCap = LineCap.Round
                            g.DrawArc(indicatorPen, CInt(thick / 2), CInt(thick / 2), imgWidth - thick - 1, imgHeight - thick - 1, 180, CInt((360 / maxValue) * indicatorValue))
                        End Using
                    End Using

                    Dim indicatorString As SizeF = g.MeasureString(CStr(CInt((100 / maxValue) * indicatorValue)), textFont)
                    g.DrawString(CStr(CInt((100 / maxValue) * indicatorValue)), textFont, Brushes.Black, CInt(imgWidth / 2 - indicatorString.Width / 2), CInt(imgHeight / 2 - indicatorString.Height / 2))
                    gr.DrawImage(bmp, 0, 0)
                End Using
            End Using

        End Sub

    End Class ' BufferDrawing

End Namespace ' RiverNile


How to use the Code:

1- add the code to your project
2- build the progress indicator control
3- add the control to your form
4- add a timer to the form
5- Add the below code to your form

Public Class Form1
    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
        Indicator1.Value += 1
        Indicator1.Invalidate()
    End Sub

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Timer1.Enabled = True
        Timer1.Start()
    End Sub

End Class



Enjoy the code from RiverNile.Net

Comments

Popular posts from this blog

مقدمة الي تشفير الحروف الأبجدية العربية

VB.NET Translucent Control using GDI+

Add Custom Event to a Class in VB.NET