Text inside a progressbar

Master Zero

Well-known member
Joined
Sep 10, 2005
Messages
51
Programming Experience
Beginner

Can anyone tell me how to put text inside a progressbar?

For example, I want to embed the current percentage value of the progressbar within it.

Thanks in advance!
 

Master Zero

Well-known member
Joined
Sep 10, 2005
Messages
51
Programming Experience
Beginner
Thanks, I was talking about the built-in control. I’ll give your ideas a try, sound like it would work.
 

kulrom

Well-known member
Joined
May 10, 2005
Messages
2,854
Location
Republic of Macedonia
Programming Experience
10+
OK here we go ... make a new project selecting a "windows control library" from templates.
Then draw a progress bar from the toolbar pane and just join next code in design view (just after 'Windows Form Designer generated code')

VB.NET:
[SIZE=2][SIZE=2][COLOR=#0000ff]Property[/COLOR][/SIZE][SIZE=2] Value() [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Integer
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Get
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Return[/COLOR][/SIZE][SIZE=2] ProgressBar1.Value
[/SIZE][SIZE=2][COLOR=#0000ff]End [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Get
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Set[/COLOR][/SIZE][SIZE=2]([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] Value [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Integer[/COLOR][/SIZE][SIZE=2])
ProgressBar1.Value = Value
UpdateLabel()
[/SIZE][SIZE=2][COLOR=#0000ff]End [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Set
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]End [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Property[/COLOR][/SIZE]
[COLOR=#0000ff][/COLOR] 
[SIZE=2][COLOR=#0000ff]
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Property[/COLOR][/SIZE][SIZE=2] Maximum() [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Integer
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Get
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Return[/COLOR][/SIZE][SIZE=2] ProgressBar1.Maximum
[/SIZE][SIZE=2][COLOR=#0000ff]End [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Get
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Set[/COLOR][/SIZE][SIZE=2]([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] Value [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Integer[/COLOR][/SIZE][SIZE=2])
ProgressBar1.Maximum = Value
[/SIZE][SIZE=2][COLOR=#0000ff]End [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Set
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]End [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Property[/COLOR][/SIZE]
[COLOR=#0000ff][/COLOR] 
[SIZE=2][COLOR=#0000ff]
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Property[/COLOR][/SIZE][SIZE=2] [Step]() [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Integer
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Get
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Return[/COLOR][/SIZE][SIZE=2] ProgressBar1.Step
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Get
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Set[/COLOR][/SIZE][SIZE=2]([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] Value [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Integer[/COLOR][/SIZE][SIZE=2])
ProgressBar1.Step = Value
[/SIZE][SIZE=2][COLOR=#0000ff]End [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Set
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]End [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Property[/COLOR][/SIZE]
[COLOR=#0000ff][/COLOR] 
[SIZE=2][COLOR=#0000ff]
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Public [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] PerformStep()
ProgressBar1.PerformStep()
UpdateLabel()
[/SIZE][SIZE=2][COLOR=#0000ff]End [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]
 
[SIZE=2][COLOR=#0000ff]
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] UpdateLabel()
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] myString [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2] = ((ProgressBar1.Value * 100) \ ProgressBar1.Maximum).ToString()
myString &= "% Done"
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] canvas [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Graphics = [/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].ProgressBar1.CreateGraphics
canvas.DrawString(myString, [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] Font("Verdana", 8, FontStyle.Regular), [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] SolidBrush(Color.Red), 90, 4)
canvas.Dispose()
[/SIZE][SIZE=2][COLOR=#0000ff]End [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub
[/COLOR][/SIZE][/SIZE]


to test the control just add the new control inside new windows project and add timer control.


timer control code:
VB.NET:
[SIZE=2][SIZE=2][COLOR=#0000ff]Private [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] Timer1_Tick([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Object, [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.EventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2] Timer1.Tick
Status.PerformStep()
[/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] Status.Maximum = Status.Value [/SIZE][SIZE=2][COLOR=#0000ff]Then[/COLOR][/SIZE][SIZE=2] Timer1.Enabled = [/SIZE][SIZE=2][COLOR=#0000ff]False
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]
[/SIZE]


If you want to reset the timer just join next code to the button or something (i will use a button for the purpose)

VB.NET:
[SIZE=2][COLOR=#0000ff]Private [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] Button2_Click([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Object, [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.EventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2] Button2.Click
Timer1.Enabled = [/SIZE][SIZE=2][COLOR=#0000ff]False
[/COLOR][/SIZE][SIZE=2]Status.Value = 0
Status.Maximum = 20
Status.Step = 1
Timer1.Enabled = [/SIZE][SIZE=2][COLOR=#0000ff]True
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]End [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub
[/COLOR][/SIZE]


HTH
Regards ;)

P.S. do not hesitate to ask for a demo project if you cannot realize it from the code above :)
 

kulrom

Well-known member
Joined
May 10, 2005
Messages
2,854
Location
Republic of Macedonia
Programming Experience
10+
Ok this is a demo project as well as custom control code (project) ... ask if you need an additional help :)


Regards ;)
 

Attachments

  • PercentageProgressbarDEMO.zip
    30.9 KB · Views: 762
  • ProgressbarWithpercenatgeDLL.zip
    24.7 KB · Views: 620
  • demo.png
    demo.png
    7.6 KB · Views: 1,018

Brainbug

Member
Joined
Dec 29, 2005
Messages
17
Location
Hanover, Germany
Programming Experience
5-10
Thanks so far!

Is it possible to get self-made controls in the toolbox, to pick and drapg&drop them to a form, when needed? How did you get your UserControl in there?

Brainbug
 

kulrom

Well-known member
Joined
May 10, 2005
Messages
2,854
Location
Republic of Macedonia
Programming Experience
10+
Brainbug said:
Is it possible to get self-made controls in the toolbox, to pick and drapg&drop them to a form, when needed? How did you get your UserControl in there?

Brainbug

just as you described. Through the toolbox "add/remove items..." ...just locate the .dll file inside bin folder of the 2nd attached project above and click OK. Now you can simply drag that to the form ...

HTH
Regards ;)
 

Brainbug

Member
Joined
Dec 29, 2005
Messages
17
Location
Hanover, Germany
Programming Experience
5-10
finally found it...

I'm using .net 2006 and there it says "choose items" and then you have to browse for the dll; now I managed it to use your control. Thank you very much for your support.

Brainbug
 

kulrom

Well-known member
Joined
May 10, 2005
Messages
2,854
Location
Republic of Macedonia
Programming Experience
10+
Brainbug said:
I'm using .net 2006 and there it says "choose items" and then you have to browse for the dll; now I managed it to use your control. Thank you very much for your support.

Brainbug

Is there 2006 version already? Then i am not very well informed ... JK :) .. suppose typo but, anyway you are wellcome for help ..anytime, if i am able to be of help ... Regards ;)
 

Nighty01

New member
Joined
Mar 15, 2006
Messages
1
Programming Experience
10+
Update (show Any texts)

I changed a little bit, so you can set any kind of text in the progress bar.

Private Sub UpdateLabel(ByVal PB1 As ProgressBar, ByVal sInputString As String, Optional ByVal oFont As Font = Nothing)
Dim sString As String
Dim canvas As Graphics = PB1.CreateGraphics
Dim iWidth As Integer
If IsNothing(oFont) Then
oFont = New Font("Verdana", 8, FontStyle.Bold)
End If
sString = ((PB1.Value * 100) \ PB1.Maximum).ToString()
sString &= "% " + sInputString
iwidth = (PB1.Width - canvas.MeasureString(sString, oFont).Width) \ 2
canvas.DrawString(sString, oFont,
New SolidBrush(Color.White), iwidth + 1, 1) ' Not like a 3D effect, so please delete this line.
canvas.DrawString(sString, oFont,
New SolidBrush(Color.Red), iwidth, 0)
canvas.Dispose()
End Sub

regrards

Nighty :cool:

 

qaball

New member
Joined
Oct 6, 2006
Messages
2
Programming Experience
1-3
Very cool, but how do I use it?

I like the ability to add text to the progress bar but I have just 2 problems.

1. How do I construct the control to allow me to pass the text to it as a parameter? I'm using the controil code from the examples above but I don't know enough about constructing custom controls to get this to work.

2. How do I change the color of the progress bar? Nothing I do in the DEMO program included above lets me change the color of the progress. I can see how to change the font, size and color but not the bar behind it.

Any help would be greatly appreciated!

Thanks
qaball
 

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,540
Location
Norway
Programming Experience
10+
qaball, a usercontrol is built by placing one or more common controls inside it. If you want properties of the usercontrol itself to access properties of the individual controls behind you must write these properties yourself, providing the link to and fro the usercontrol level and child control level. You may create all new properties of any your liking or override the behaviour of the existing overridable usercontrol properties.
 

qaball

New member
Joined
Oct 6, 2006
Messages
2
Programming Experience
1-3
Almost got it.

JohnH

I have it working and cleaned up and it works like a charm now. I do have one question, since I've been working with the demo code that has been posted and used that as a starting point, does anyone know why the bars are green and not the blue of the progressbar control included with VB? Is that an affectation of it being a usercontrol?

Thanks
qaball
 

[RTS]BN+VS*

Well-known member
Joined
Feb 3, 2009
Messages
53
Location
BELGIUM - East Flanders
Programming Experience
1-3
Hey,

I'm using my own adaptation of this code, but I've run into a problem.
If you draw the string onto the progressbar, it'll be removed on vista every x secs cause there is some refresh indicator going through the bar.

Is there any event (or procedure used that is overidable), which occurs then, so the string can be redrawn?

Either a solution to this question or a link to a working control that does not have this problem would be very much appreciated.

Cheers
BN
 

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,540
Location
Norway
Programming Experience
10+
You draw in Paint event and you use the provided graphics instance e.Graphics to draw with.
 

[RTS]BN+VS*

Well-known member
Joined
Feb 3, 2009
Messages
53
Location
BELGIUM - East Flanders
Programming Experience
1-3
Yes, that's the logical approach. But apparently the pain event isn't fired every time this bar updates (I have already tried this out by putting a messagebox in it). So putting gfx creation there will have no result whatsoever except for startup :(
 

[RTS]BN+VS*

Well-known member
Joined
Feb 3, 2009
Messages
53
Location
BELGIUM - East Flanders
Programming Experience
1-3
Could you provide me with a small example of how you would do it? Maybe I'm just handling it the wrong way.

I've added my current code as attachment.
 

Attachments

  • pbar.txt
    4.6 KB · Views: 126

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,540
Location
Norway
Programming Experience
10+
You don't put message boxes in Paint event. I also forgot Progressbar doesn't have a Paint event for use, have a look at this example on hwo you can draw text to progressbar when WM_PAINT messages are received: post 5 of thread Draw Text with Blend to background
 

[RTS]BN+VS*

Well-known member
Joined
Feb 3, 2009
Messages
53
Location
BELGIUM - East Flanders
Programming Experience
1-3
Hey,

Your example helped me through this problem :)
I tried overriding a whole bunch of look-like-they-have-something-to-do-with-the-paint-event methods, but had no idea WndProc was the correct one xD

Cheers
BN
 
Last edited:
Top Bottom