Link to home
Start Free TrialLog in
Avatar of Zharphyn
ZharphynFlag for Canada

asked on

Placing a label on a Progress Bar

I am writing a windows program, and on one form, I have a progress bar.  I found some code to write a label over the progress bar to indicate percentage, and the code works, when percentage is 0%.  Once the progress bar value is greater than 0, the progress bar has an animation that runs every second.  This animation causes my label to be hidden.  
    Private Sub SetProgressBarText()
        Dim szPercent As String
        Dim szTextString As String

        szPercent = CInt(((pbrTimeRemaining.Value - pbrTimeRemaining.Minimum) / (pbrTimeRemaining.Maximum - pbrTimeRemaining.Minimum)) * 100).ToString & "%"
        szTextString = "Training Time Used - " & szPercent
        Dim g As Graphics = pbrTimeRemaining.CreateGraphics()
        g.DrawString(szTextString, SystemFonts.DefaultFont, Brushes.Black, _
                     New PointF(pbrTimeRemaining.Width / 2 - (g.MeasureString(szTextString, SystemFonts.DefaultFont).Width / 2), _
                                pbrTimeRemaining.Height / 2 - (g.MeasureString(szTextString, SystemFonts.DefaultFont).Height / 2)))
        pbrTimeRemaining.Refresh()

    End Sub

Open in new window


My question is:
What is the easiest way to have a label appear on a progress bar in VB.NET?  Or can I turn off the animation?

Thanks
Brad
Avatar of Jacques Bourgeois (James Burger)
Jacques Bourgeois (James Burger)
Flag of Canada image

Your code deals with the painting of the ProgressBar. Painting occurs everytime the bar is incremented, so your code should be called regularly. If you call it only once at the begining, the first repaint will remove the text.

Be sure to call your code each time you increment.
Avatar of Zharphyn

ASKER

I am already doing that.  What appears to be happening, is that every second, the progress bar has an animation occur, and this animation either removes or hides the text.  Is there a way to turn off this animation, or to have the animation also update the text?

Thanks
Brad
The animation might come from the fact that you have set the Style property to Marquee. In Marquee mode, there is no way to intervene, because for some reason, the ProgressBar does not trigger a Paint event as most controls do.

Check the Style, and set it to something else than Marquee if this is set.

Also, be sure to call your method after the value has been changed, not before.
The style was (and still is) set to Blocks, and the method call to set the progress bar text is after the progress bar value is changed.  But even when no change is occurring to the progress bar value, the animation occurs and wipes out the text.
    Private Sub frmEditTraining_Shown(sender As Object, e As EventArgs) Handles Me.Shown
        InitTrainingDone()
        InitTrainingDue()
        InitProgressBars()
        SetProgressBarText()

    End Sub

Open in new window

Thanks
Brad
Sorry.

I just spent an hour playing with the stuff, trying to inherit from ProgressBar in order to change the way it works, but even when inheriting, the standard event triggers (OnPaint, OnPaintBackground and the likes) that are usually used on other controls for such purposes are not fired.

You might want to try one of the numerous alternative to the standard ProgressBar that are available through a quick search through Bing or Google.
ASKER CERTIFIED SOLUTION
Avatar of Ark
Ark
Flag of Russian Federation image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you.  The custom control works great.

Thanks
Brad