Wine Too
Wine Too

Reputation: 4655

How to use a ProgressBar properly in VB.NET

I have to use a progress bar in my VB.NET programs which behaves very different from those in VB6. For example, if I have a procedure to fill a datagridview through some loop and show that progress with progressbar what happend?
Datagridview fill's 100% while progressbar comes to about 50%!

Here is example program to illustrate a problem. Create a new project, add a windows form and just copy this code on Form1's code.

Public Class Form1

Dim myMax As Integer = 100000
Dim pb As New ProgressBar
Dim dgv As New DataGridView
Dim WithEvents ti As New Timer

Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    With Me
        .Width = 400
        .Height = 250

        .Controls.Add(pb)
        With pb
            .Maximum = myMax
            .Dock = DockStyle.Bottom
        End With

        .Controls.Add(dgv)
        With dgv
            .ColumnCount = 2
            .Dock = DockStyle.Fill
            .Columns(1).AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
            .Visible = False
        End With
    End With

    ti.Start()
End Sub

Private Sub OnFormLoaded(ByVal sender As Object, ByVal e As EventArgs) Handles ti.Tick
    ti.Enabled = False
    ti.Dispose()

    Dim temp As Integer

    For t = 0 To myMax

        If t Mod 100 = 0 Then
            pb.Value = t
            pb.Refresh()
            Application.DoEvents()

            temp += 1

            dgv.Rows.Add(New String() { _
                t.ToString, _
                temp.ToString _
                })
        End If
    Next
    pb.Value = myMax
    pb.Visible = False
    dgv.Visible = True
    dgv.Focus()

End Sub
End Class

This code creates few controls, loads a form and starts a loop to fill data and show that progress in progressbar. After that program hides a progressbar and shows a datagridview what is usual situation in concrete (real-world) programs.

Problem is that although both, datagridview filling and updating a progressbar goes from same loop (in steps by 100) filling of datagridview ends much faster than progressbar show a progress and hides it on about 50%.

This is much different from VB6 where filling and showing is totally sinchronized and grid will be showed after progressbar reaches value of 100%.

How to get such functionality of progressbar in VB.NET on showed code?
I try with refresh of progressbar and DoEvents but this is not enough to get it work as expected.

Upvotes: 4

Views: 55482

Answers (1)

user1191877
user1191877

Reputation:

In order to solve this problem without to do a "threaded science fiction" from just a ProgressBar you have to use one technique which is often with microsoft's GUI toolkits.

Such approach can probably solve your concrete problem:

    If t Mod 100 = 0 Then
        pb.Value = t
     If pb.Value > 0 Then pb.Value -= 1

        temp += 1

        dgv.Rows.Add(New String() { _
            t.ToString, _
            temp.ToString _
            })
    End If

Upvotes: 2

Related Questions