title: How to use a ProgressBar properly in VB.NET
body: <p>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?<br>
  Datagridview fill's 100% while progressbar comes to about 50%!  </p>
  
  <p>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.</p>
  
  <pre><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
  </code></pre>
  
  <p>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.   </p>
  
  <p>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%.</p>
  
  <p>This is much different from VB6 where filling and showing is totally sinchronized and grid will be showed after progressbar reaches value of 100%.  </p>
  
  <p>How to get such functionality of progressbar in VB.NET on showed code?<br>
  I try with refresh of progressbar and DoEvents but this is not enough to get it work as expected.</p>