Visual Basic Common Control API Routines
SetParent: Display Modal Form Activity in a Parent Form's VB ProgressBar
     
Posted:   Thursday June 29, 1999
Updated:   Monday December 26, 2011
     
Applies to:   VB4-32, VB5, VB6
Developed with:   VB6, Windows NT4
OS restrictions:   None
Author:   VBnet - Randy Birch
     

Related:  

SetParent: Add a VB Progress Bar to a VB StatusBar
SendMessage: Change the Colour of a VB ProgressBar
Pure VB: Customizable PhotoShop-Style ProgressBar
Pure VB: Customizable PhotoShop-Style ProgressBar in a MDI App
CreateWindowEx: Creating a Common Control ProgressBar- Overview
CreateWindowEx: Creating the Common Control Flood Panel via the API
SetParent: Display Modal Form Activity on a Parent Form's 'PhotoShop-style' ProgressBar
SetParent: Display Modal Form Activity in a Parent Form's VB ProgressBar
     
 Prerequisites
None.

This is simple code that demonstrates how to use a normal VB progress bar on a parent form (SDI or MDI) to track the activity being performed in another form, even when that form is modal. 

The demo also shows how to call SetParent to embed the progress bar inside the status bar panel.

 BAS Module Code
None.

 Form Code - Parent Form
Create two new forms.

To the main 'parent' form, add a command button (Command1), a status bar (StatusBar1) with at least 1 panel, and a common control progress bar (ProgressBar1).

To the 'child' modal form, add a command button (Command1). 

Add the following code to the 'Parent' (calling) form:


Option Explicit
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Copyright ©1996-2011 VBnet/Randy Birch, All Rights Reserved.
' Some pages may also contain other copyrights by the author.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Distribution: You can freely use this code in your own
'               applications, but you may not reproduce 
'               or publish this code on any web site,
'               online service, or distribute as source 
'               on any media without express permission.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Private Declare Function SetParent Lib "user32" _
  (ByVal hWndChild As Long, _
   ByVal hWndNewParent As Long) As Long



Private Sub Form_Load()

   Me.Move (Screen.Width - Me.Width) \ 2, _
           (Screen.Height - Me.Height) \ 2
   
   SetParent ProgressBar1.hWnd, StatusBar1.hWnd
   
   With ProgressBar1
      .Visible = False
      .Align = vbAlignNone
      .Appearance = ccFlat
      .BorderStyle = ccNone
      .Move 40, _
            40, _
            StatusBar1.Panels(1).Width - 40, _
            StatusBar1.Height - 40
               
      .ZOrder 1
   End With

   StatusBar1.Panels(1).Text = "ProgressBarwill appear here."
   
End Sub


Private Sub Command1_Click()

   Command1.Enabled = False
   
   With Form2
      Set .ParentForm = Me
      .Show vbModal
   End With
   
   Command1.Enabled = True

End Sub


Public Sub ProgBarInitialize(pbMin As Integer, _
                          pbMax As Integer, _
                          pbInitValue As Integer)
   
   With ProgressBar1
      .Max = pbMax
      .Min = pbMin
      .Value = pbInitValue
      .ZOrder 0
      .Visible = True
   End With

End Sub


Public Sub ProgBarTerminate()
   
   ProgressBar1.Visible = False

End Sub
 Form Code - Modal (Child) Form
Add the following code to the Child form:

Option Explicit
Private frmParent As Form


Private Sub Form_Load()

   Me.Move (Screen.Width - Me.Width) \ 2, _
           (Screen.Height - Me.Height) \ 2
           
End Sub


Private Sub Command1_Click()

   Dim cnt As Integer
   
  'initialize the progress bar:
  'min = 0, max = 5000, initial = 0
   frmParent.ProgBarInitialize 0, 5000, 0
   
   For cnt = 1 To 5000
   
     '[activity code here]
   
     'show progress
      frmParent.ProgressBar1.Value = cnt
      DoEvents
   
   Next

   frmParent.ProgBarTerminate
   
End Sub


Public Property Set ParentForm(ByVal frm As Form)

   Set frmParent = frm

End Property


Private Sub Form_Unload(Cancel As Integer)

   Set frmParent = Nothing
   
End Sub
 Comments
When run, the progress bar in the parent form will update based on the modal child's activity.

 
 

PayPal Link
Make payments with PayPal - it's fast, free and secure!

 
 
 
 

Copyright ©1996-2011 VBnet and Randy Birch. All Rights Reserved.
Terms of Use  |  Your Privacy

 

Hit Counter