Progress Bar for Loop

Posted on 2005-03-07
Medium Priority
Last Modified: 2007-12-19
I have a loop that send out customised SMS messages to clients, as this loop can take anything from 20 minutes to 1 hour I need to integrate a progress bar to monitor the progress of the loop... Ideally I would like to place it in the Status Bar of the MDI Parent Form... Any ideas?
Question by:odonovanpm
LVL 14

Expert Comment

by:Thandava Vallepalli
ID: 13475970
LVL 11

Accepted Solution

Ramesh Srinivas earned 336 total points
ID: 13475980
LVL 14

Assisted Solution

by:Thandava Vallepalli
Thandava Vallepalli earned 332 total points
ID: 13476007
LVL 27

Assisted Solution

planocz earned 332 total points
ID: 13477716
Sample code...


Public Class Form1
    Inherits System.Windows.Forms.Form
    Friend spb As StatusProgressBar
#Region " Windows Form Designer generated code "

    Public Sub New()

        'This call is required by the Windows Form Designer.

        '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
            End If
        End If
    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 Button1 As System.Windows.Forms.Button
    Friend WithEvents Status As System.Windows.Forms.StatusBar
    Friend WithEvents StatusBarPanel1 As System.Windows.Forms.StatusBarPanel
    Friend WithEvents pgb As System.Windows.Forms.ProgressBar
    Friend WithEvents StatusBarPanel2 As System.Windows.Forms.StatusBarPanel
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.Button1 = New System.Windows.Forms.Button
        Me.Status = New System.Windows.Forms.StatusBar
        Me.StatusBarPanel1 = New System.Windows.Forms.StatusBarPanel
        Me.StatusBarPanel2 = New System.Windows.Forms.StatusBarPanel
        Me.pgb = New System.Windows.Forms.ProgressBar
        CType(Me.StatusBarPanel1, System.ComponentModel.ISupportInitialize).BeginInit()
        CType(Me.StatusBarPanel2, System.ComponentModel.ISupportInitialize).BeginInit()
        Me.Button1.Location = New System.Drawing.Point(200, 64)
        Me.Button1.Name = "Button1"
        Me.Button1.TabIndex = 0
        Me.Button1.Text = "Button1"
        Me.Status.Location = New System.Drawing.Point(0, 244)
        Me.Status.Name = "Status"
        Me.Status.Panels.AddRange(New System.Windows.Forms.StatusBarPanel() {Me.StatusBarPanel1, Me.StatusBarPanel2})
        Me.Status.ShowPanels = True
        Me.Status.Size = New System.Drawing.Size(400, 22)
        Me.Status.TabIndex = 1
        Me.Status.Text = "StatusBar1"
        Me.StatusBarPanel1.Style = System.Windows.Forms.StatusBarPanelStyle.OwnerDraw
        Me.StatusBarPanel1.Text = "StatusBarPanel1"
        Me.StatusBarPanel2.MinWidth = 100
        Me.StatusBarPanel2.Text = "StatusBarPanel2"
        Me.StatusBarPanel2.Width = 10000
        Me.pgb.Location = New System.Drawing.Point(192, 152)
        Me.pgb.Maximum = 10000
        Me.pgb.Name = "pgb"
        Me.pgb.Size = New System.Drawing.Size(168, 24)
        Me.pgb.Step = 1
        Me.pgb.TabIndex = 2
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(400, 266)
        Me.IsMdiContainer = True
        Me.Name = "Form1"
        Me.Text = "Form1"
        CType(Me.StatusBarPanel1, System.ComponentModel.ISupportInitialize).EndInit()
        CType(Me.StatusBarPanel2, System.ComponentModel.ISupportInitialize).EndInit()

    End Sub

#End Region
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        ' Make a loop to simulate something is going on
        ' spb.ProgressBar.Value = 0
        With spb.ProgressBar
            .Maximum = 10000
            .Minimum = 1
            .Value = 1
            .Step = 1
        End With
        Dim lngI As Long
        For lngI = 1 To spb.ProgressBar.Maximum
            ' The progressbar's properties are not default: maximum = 10000, step = 1
    End Sub
    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ' Declare the StatusProgressBar. Give the progressbar, the statusbar and the panel index as arguments.
        spb = New StatusProgressBar(pgb, Status, 1)
    End Sub
    Private Sub Form1_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Resize
        ' Use the resize method to resize the object. Check wether the object exists for two reasons:
        ' - this is a good technique to allow disposing the object when necessary
        ' - during the creation of the form, resize is called before load, so the object does not exist yet
        If Not spb Is Nothing Then
        End If
    End Sub
End Class


Class StatusProgressBar

    Public ProgressBar As ProgressBar    ' The progressbar
    Public StatusBar As StatusBar        ' The Statusbar
    Public Panel As Int16                ' The status panel
    Public Margin As Int32               ' The margin between the panel's edge and the progressbar

    ' API SendMaessageA in User32: returns the rectangle (RECT structure) used to display a status-bar panel
    ' Parameters are the statusbar's handle, the "Get Rectangle" message, the panel's number, and the RECT structure to return the values
    Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Int32, ByVal msg As Int32, ByVal wParam As Int32, ByRef lParam As RECT) As Long
    Private Const WM_USER As Long = &H400
    Private Const SB_GETRECT As Long = (WM_USER + 10)
    ' RECT structure to get the panel's display rectangle
    Private Structure RECT
        Friend Left As Int32
        Friend Top As Int32
        Friend Right As Int32
        Friend Bottom As Int32
    End Structure
    Public Sub New(ByRef pgb As ProgressBar, ByRef sb As StatusBar, Optional ByRef intPanelNumber As Int16 = 0, Optional ByVal intMargin As Int32 = 2)
        ' Puts a progress bar in a statusbar's panel
        ' Affects the progressbar to the class members
        ProgressBar = pgb
        StatusBar = sb
        Panel = intPanelNumber
        ' Sets the margin between the statusbar panel edge and the progressbar
        Margin = intMargin
        ' Reparent it
        ProgressBar.Parent = StatusBar
        ' Set the size
        ' Set it visible
        ProgressBar.Visible = True
    End Sub
    Public Sub Resize()
        'Fits the progress bar to the status bar
        Dim Rectangle As RECT
        ' Use the API to get the panel's display rectangle
        SendMessage(StatusBar.Handle.ToInt32, SB_GETRECT, Panel, Rectangle)
        ' Resize
        With ProgressBar
            .Left = Rectangle.Left + Margin
            .Top = Rectangle.Top + Margin
            .Height = Rectangle.Bottom - Rectangle.Top - 2 * Margin
            .Width = Rectangle.Right - Rectangle.Left - 2 * Margin
        End With
    End Sub
End Class
LVL 13

Expert Comment

by:Corey Scheich
ID: 13495917
In your Child form

Public Event StatusChange (Count as long, Total as long)

Sub RunForLoop ()
Dim SomeArray(8) as String

for c = 0 to ubound(someArray)
        'do something
        Raiseevent StatusChange (c,ubound(someArray))
        msgbox("You Should See " & str(c+1) & " bar(s)"
end sub

In your Parent form

Sub ChildFormStatusChangeEvent ( count as long, total as long) handles Childform1.StatusChange

progressbar1.Maximum = Total
Progressbar1.value = count

end sub


Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
This video tutorial shows you the steps to go through to set up what I believe to be the best email app on the android platform to read Exchange mail.  Get the app on your phone: The first step is to make sure you have the Samsung Email app on your …
Suggested Courses

621 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question