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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 14

Expert Comment

by:Thandava Vallepalli
ID: 13475970
LVL 11

Accepted Solution

saleek 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

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

Technology Partners: 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

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses

770 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