can I have 2 form share the same progress bar? If so how does this work?

Recommended Answers

All 3 Replies

You mean there's a progressbar that both form's can update?

Yes, it's possible. Create a project with two forms (Form1 and Form2), drop a button to both forms (Button1) and a progressbar (ProgressBar1) to Form1. Paste the following code to Form1:

Public Sub SetupProgressBar(ByVal MinValue As Integer, ByVal MaxValue As Integer, ByVal StepValue As Integer)
    ' Form1 contains this progressbar
    If StepValue > 0 AndAlso MinValue < MaxValue Then
      Me.ProgressBar1.Minimum = MinValue
      Me.ProgressBar1.Maximum = MaxValue
      Me.ProgressBar1.Value = MinValue
      Me.ProgressBar1.Step = StepValue
    Else
      ' Use soome default values
      Me.ProgressBar1.Minimum = 0
      Me.ProgressBar1.Maximum = 100
      Me.ProgressBar1.Value = 0
      Me.ProgressBar1.Step = 1
    End If

  End Sub

  Public Sub UpdateProgressBar(ByVal NewValue As Integer)
    ' Update progress bar
    If NewValue >= Me.ProgressBar1.Minimum AndAlso NewValue <= Me.ProgressBar1.Maximum Then
      Me.ProgressBar1.Value = NewValue
    End If

  End Sub

  Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    Form2.Show()

  End Sub

and this to Form2:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    ' Use Form1's progress bar
    Dim i As Integer

    Form1.SetupProgressBar(0, 100, 10)
    For i = 0 To 100
      Form1.UpdateProgressBar(i)
      Application.DoEvents()
      Threading.Thread.Sleep(150)
    Next i

  End Sub

The point is that the line 7. Form1.UpdateProgressBar(i) calls from the second form a procedure in Form1 that updates the progressbar. You could also reference the progressbar directly with Form1.ProgressBar1.Value but "wrapping" updating and validity checking in a single procedure makes the code easier to maintain. In the Form1 you don't need to qualify the update procedure so you call it UpdateProgressBar(<value>).

HTH

and also, is it possible to pass the progress bar as a function parameter?

Yes it is.

Private _SharedProgressBar As ProgressBar

  Public Sub SetSharedProgressBar(ByRef value As ProgressBar)
    ' Pass a progressbar as a parameter
    _SharedProgressBar = value

  End Sub

Just remember that you still have one instance of progressbar i.e. changing _SharedProgressBar properties reflect to progressbar you passed as an argument.

Here's a more general way to use controls:

Private _SharedControl As Control

  Public Sub SetSharedControl(ByRef value As Control)
    ' Pass a control as a parameter
    _SharedControl = value

  End Sub

  Private Sub UseSharedControl()
    ' An example how to use a control (of type progressbar)
    CType(_SharedControl, ProgressBar).Value = 10

  End Sub

Just for the reference.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.