can I have 2 form share the same progress bar? If so how does this work?
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>).
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.
Hi, as I was told that my code doesn’t scale well at all, I thought perhaps I’d try to get a better understanding of interfaces/abstract classes and classes and the relationship between them.
I don’t want at this stage work on a big separate project as I've already got plenty ...
OK, so HostGator for some reason no longer allows gcc/g++ access unless you have a Designated Server account, which is a lot of money to spend just to compile my "Hello World" program. Thus I figured I'd compile at home, then upload. Program is your regular old bare-bones Hello World ...
Hi. I have a form with list box : lst_product, datagridview : grd_order and button: btn_addline. lst_product has a list of product ids selected from database (MS Acess 2013) , grd_order is by default empty except for 2 headers and btn_addline adds rows to grd_order.
Private Sub btn_addline_Click(ByVal ...