Solved

Placing a label on a Progress Bar

Posted on 2014-02-04
7
2,497 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

Expert Comment

by:Jacques Bourgeois (James Burger)
Comment Utility
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
Comment Utility
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

Expert Comment

by:Jacques Bourgeois (James Burger)
Comment Utility
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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 

Author Comment

by:Zharphyn
Comment Utility
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

Expert Comment

by:Jacques Bourgeois (James Burger)
Comment Utility
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
Comment Utility
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
Comment Utility
Thank you.  The custom control works great.

Thanks
Brad
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

772 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

11 Experts available now in Live!

Get 1:1 Help Now