i want to add a label over progress bar to show the percentage of the completed task .

i added the code :

            Label1.Parent = ProgressBar1
            Label1.BackColor = Color.Transparent

but now i can't see the label1 even.
so how to make label1 transparent.

VB.NET doesnt support transparent labels. You will have to draw the text over it using the Graphics.DrawString or some kind of custom control.

Microsoft decided that we didn't need this function. >(

You can use transparent panels and such, but when you set them to transparent - the label will still have a boarder around your text.

so would you provide code using Graphics. Drawstring to draw text over progressbar with center aligned ?

Something like this might help:

Private Sub WriteToProgressBar(ByVal pbIn As ProgressBar, ByVal sTextToDraw As String, ByVal f As Font, ByVal b As Brush)
    Try
        Using g As Graphics = Me.CreateGraphics
            'Check to see if text is to wide/tall
            If sTextToDraw.Length * f.Size > pbIn.Width Or _
               f.Height > pbIn.Height Then
                Throw New ArgumentException("Text is to large for progress bar!")
            Else
                Dim iTryGetCenterW As Integer = (pbIn.Width / 2) - ((sTextToDraw.Length * f.Size) / 2)
                Dim iTryGetCenterH As Integer = (pbIn.Height / 2) - (f.Size / 2)
                g.DrawString(sTextToDraw, f, b, New Point(iTryGetCenterW, iTryGetCenterH))
            End If
        End Using
    Catch ex As Exception
        MsgBox(ex.ToString)
    End Try
End Sub

You will have to take the progress bar's location into consideration as well.
But you must take the progress bar's location into consideration.

ok
i tried to put that code directly inside timer instead of putting inside a user defined procedure

so i put

Dim g As Graphics = Me.CreateGraphics
g.DrawString(ProgressBar1.Value & "%", New Font("Arial"), Brushes.Aqua, New Point(100, 100))

and it returns an error saying
Overload resolution failed because no accessible 'New' accepts this number of arguments.

The font constructor requires a font size. (Em Size)

It's going to be a problem because every time you call g.DrawString it will write overtop of the existing text as the numbers change you will end up with just a black blob. Previously rendered numbers will not be erased. You can find a custom progress bar at Code Project which might do what you want.

commented: Yep +8

It's a pity Microsoft didn't provide a Text property with the standard ProgressBar instead of leaving it up to everyone to roll their own.

Hi had the same problem with the ProgressBar is pretty complicated, so I ended up creating my own BAR.
I used a Label as bar, because I can give it the size I want, put tekst in it, and give it the Back- and ForeColor I want. Also a Timer to Make events happen. As it is it counts Up to any number of seconds, with a
few changes in the code it will count Down from any number of seconds. Have not tried it but must be possible to use with bytes or so as well. The code below is what I wrote for testing my BAR. Maybe it helps you.

Public Class Form1
    Dim counttill As Integer = 180   ' change to any value for seconds to count
    Dim barwidth As Integer = 500   ' change to any value for maximum width of Label1
    Dim barsize As Double = 0
    Dim nextpixel As Double = 0

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        nextpixel = (barwidth / counttill).ToString   'calculate number of pixels to add with each step
        Label1.Width = 0
        Timer1.Interval = 1000
        Timer1.Enabled = True
    End Sub

    Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
        counttill += 1
        barsize = barsize + nextpixel
        Label1.Width = barsize
        Dim prcnt As Integer = barwidth / 100   ' where prcnt is 1% of maximum barwidth
        Dim percent As Integer = barsize / prcnt   ' actual barwidth in %
        Label1.Text = percent & "%"
        Label2.Text = barsize   ' not needed is just for checking
        If percent = 100 Then
            Timer1.Enabled = False
        End If
    End Sub

End Class

Tried it another way, used the ProgressBar to make a Label scroll like it, works ok for me.
Works with a Trackbar as well. I added this code to a ProgressBar routine. Where xxx = maximum width for label1, used same size as for ProgressBar. Percentage can be put in like shown before.

Label1.Width = ProgressBar1.Value * (xxx / ProgressBar1.Maximum)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.