?
Solved

Placing a label on a Progress Bar

Posted on 2014-02-04
7
Medium Priority
?
2,931 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
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…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Suggested Courses

764 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