Form Progressbar using Graphics in VB.NET

Introduction:

For last several weeks, I have been thinking about a progress bar which could display percentage progress of the work done but despite numerous efforts I was unable to do so as the existing progress bar control available in Visual studio.NET has no such property to display Text neither the progress bar is inheritable on User-Controls so the addition of properties and methods in the Progress Bar was not possible. Therefore, I decided to use some other method /technique to accomplish the task easily.

Development Platform:

  • Microsoft Visual Basic.NET 2003 and Windows xp
  • Label and Graphics. 

How does it work?

I have included all the code required for building the progress bar. The user is required to set all these properties of controls and declare the desired variables.

I have used Timer Control to test the application; however, it can be substituted with loop.

Code:

Public Class frmGraphics

    Inherits System.Windows.Forms.Form

    Dim G As Graphics

    Dim Idx As Integer = 1

    Dim iPercent As Integer = 1

    Dim sPercent As String = ""

    Dim L As Label = New Label

   

#Region " Windows Form Designer generated code "

 

    Public Sub New()

        MyBase.New()

 

        'This call is required by the Windows Form Designer.

        InitializeComponent()

 

        'Add any initialization after the InitializeComponent() call

 

    End Sub

 

    'Form overrides dispose to clean up the component list.

    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)

        If disposing Then

            If Not (components Is Nothing) Then

                components.Dispose()

            End If

        End If

        MyBase.Dispose(disposing)

    End Sub

 

    'Required by the Windows Form Designer

    Private components As System.ComponentModel.IContainer

 

    'NOTE: The following procedure is required by the Windows Form Designer

    'It can be modified using the Windows Form Designer. 

    'Do not modify it using the code editor.

    Friend WithEvents T As System.Windows.Forms.Timer

    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

        Me.components = New System.ComponentModel.Container

        Me.T = New System.Windows.Forms.Timer(Me.components)

        '

        'T

        '

        Me.T.Enabled = True

        Me.T.Interval = 10

        '

        'frmGraphics

        '

        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)

        Me.ClientSize = New System.Drawing.Size(292, 273)

        Me.Name = "frmGraphics"

 

    End Sub

 

#End Region 

 

    Private Sub frmGraphics_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        '''Timer settings'''

        T.Enabled = True

        T.Interval = 100

        T.Start()

        ''''Label and color settings''

        L.Height = 15

        L.Width = 30

        L.TextAlign = ContentAlignment.MiddleRight

        L.Top = 10

        L.Left = 5

        L.BackColor = Color.Gray

        Me.BackColor = Color.Gray

        Me.Controls.Add(L)    

 

    End Sub

 

    Private Sub T_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles T.Tick

 

        If Idx >= Me.Width Then

            Idx = 1 

        End If

 

        Idx += 1 

 

        iPercent = CInt(Idx * 100 / Me.Width)

        sPercent = iPercent & "%"

 

        G = Me.CreateGraphics

        G.FillEllipse(Brushes.Red, Idx, 10, 8, 15)

        L.Left = Idx + 5

        L.Text = sPercent 

 

    End Sub 

  

End Class


Similar Articles