?
Solved

Placing a label on a Progress Bar

Posted on 2014-02-04
7
Medium Priority
?
3,111 Views
Last Modified: 2014-02-06
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
0
Comment
Question by:Zharphyn
  • 3
  • 3
7 Comments
 
LVL 40
ID: 39833513
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.
0
 

Author Comment

by:Zharphyn
ID: 39833567
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
0
 
LVL 40
ID: 39833607
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.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Zharphyn
ID: 39833668
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
0
 
LVL 40
ID: 39834461
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.
0
 
LVL 28

Accepted Solution

by:
Ark earned 2000 total points
ID: 39838328
Public Class CustomPogress
    Inherits ProgressBar

    Public Property ShowPercents As Boolean
    Public Property LabelFormat As String
    Private Const WmPaint = 15
    Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
        MyBase.WndProc(m)
        If m.Msg = WmPaint Then
            UpdateText()

        End If
    End Sub

    Private Sub UpdateText()
        Dim s As String

        If (ShowPercents) Then
            Dim percent = CInt((Value - Minimum) * 100 / (Maximum - Minimum))
            If String.IsNullOrEmpty(LabelFormat) Then
                s = percent.ToString & "%"
            Else
                s = String.Format(LabelFormat, percent)
            End If
        Else
            If (String.IsNullOrEmpty(LabelFormat)) Then
                Return
            Else
                s = LabelFormat
            End If
        End If
        Using gr = Me.CreateGraphics()
            Dim textSize = gr.MeasureString(s, Font)
            Using br = New SolidBrush(ForeColor)
                gr.DrawString(s, Font, Brushes.Black, Width / 2 - textSize.Width / 2, Height / 2 - textSize.Height / 2)
            End Using
        End Using
    End Sub

Open in new window

Sample using
  Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        CustomPogress1.LabelFormat = "Training Time Used - {0}"
        CustomPogress1.ShowPercents = True
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        For i = 1 To 100
            CustomPogress1.Value = i
            Threading.Thread.Sleep(100)
        Next
    End Sub

Open in new window

0
 

Author Closing Comment

by:Zharphyn
ID: 39839926
Thank you.  The custom control works great.

Thanks
Brad
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses
Course of the Month14 days, 18 hours left to enroll

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question