Solved

Placing a label on a Progress Bar

Posted on 2014-02-04
7
2,566 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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 27

Accepted Solution

by:
Ark earned 500 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

920 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now