Results 1 to 2 of 2

Thread: Realtime showing percentage in progressbar

  1. #1

    Thread Starter
    Member
    Join Date
    Feb 2012
    Posts
    49

    Realtime showing percentage in progressbar

    Hello friends, I am trying to resolve real time showing of percentage inside a progress bar. All what I want is to showing percentage with constant speed. Problem is in background processes(some calculations), so populating of progress bar starts when processes are finished. I want that timer starts at the moment when I press button for calculation and to finish when calculation is finished. Here is a code, but my progress bar is late, I don't know why, although I put command that timer starts before all calculations??
    Code:
    Private Sub UpdatePBar()
    
            Dim x As Single
            Dim y As Single
            Dim percentage As String = CType((ProgressBar1.Value / ProgressBar1.Maximum * 100), Integer).ToString & "%"
            Dim gr As Graphics = Me.ProgressBar1.CreateGraphics
    
            Dim sz As SizeF = gr.MeasureString(percentage, Me.ProgressBar1.Font, Me.ProgressBar1.Width)
    
            x = (Me.ProgressBar1.Width / 2) - (sz.Width / 2)
            y = (Me.ProgressBar1.Height / 2) - (sz.Height / 2)
    
            gr.DrawString(percentage, ProgressBar1.Font, Brushes.Black, x, y)
    
        End Sub
    
        Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TimerProgress.Tick
    
            Me.ProgressBar1.Increment(1)
    
            Me.ProgressBar1.Refresh()
            UpdatePBar()
    
            If Me.ProgressBar1.Value = Me.ProgressBar1.Maximum Then TimerProgress.Enabled = False
    
        End Sub
    Thanks

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Realtime showing percentage in progressbar

    You should not be doing the drawing from the Tick event handler. Basically NEVER call CreateGraphics. You should pretty much ALWAYS do your drawing in the Paint event handler of the control your drawing on, or a method called from it. You then use the Graphics object provided to you by that event. That way, your drawing gets done every time the control is painted, whenever that may be.

    That said, I doubt that you can draw on a ProgressBar if visual styles are enabled and maybe even if they're not. You may well find that you will have to create your own custom control to get that to work.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width