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!
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!
are you asking about the vs.net built-in control or custom progressbar? There should not be much differences anyway. Say you can add label control over and set its background to transparent ... then simply show the percentage.
Regards![]()
Thanks, I was talking about the built-in control. I’ll give your ideas a try, sound like it would work.
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')
Code:Property Value() As Integer Get Return ProgressBar1.Value End Get Set(ByVal Value As Integer) ProgressBar1.Value = Value UpdateLabel() End Set End Property Property Maximum() As Integer Get Return ProgressBar1.Maximum End Get Set(ByVal Value As Integer) ProgressBar1.Maximum = Value End Set End Property Property [Step]() As Integer Get Return ProgressBar1.Step EndGet Set(ByVal Value As Integer) ProgressBar1.Step = Value End Set End Property Public Sub PerformStep() ProgressBar1.PerformStep() UpdateLabel() End Sub PrivateSub UpdateLabel() Dim myString As String = ((ProgressBar1.Value * 100) \ ProgressBar1.Maximum).ToString() myString &= "% Done" Dim canvas As Graphics = Me.ProgressBar1.CreateGraphics canvas.DrawString(myString, New Font("Verdana", 8, FontStyle.Regular), New SolidBrush(Color.Red), 90, 4) canvas.Dispose() End Sub
to test the control just add the new control inside new windows project and add timer control.
timer control code:
Code:Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick Status.PerformStep() If Status.Maximum = Status.Value Then Timer1.Enabled = False EndSub
If you want to reset the timer just join next code to the button or something (i will use a button for the purpose)
Code:Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click Timer1.Enabled = False Status.Value = 0 Status.Maximum = 20 Status.Step = 1 Timer1.Enabled = True End Sub
HTH
Regards![]()
P.S. do not hesitate to ask for a demo project if you cannot realize it from the code above
I need a demo how to manage that new control and how to put it into a new project. By the way: why DOES the "transparent"-Backcolor NOT work with the Label and the ProgressBar (as I stated here: Transparent Labels)
Brainbug
Ok this is a demo project as well as custom control code (project) ... ask if you need an additional help
Regards![]()
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 ...Originally Posted by Brainbug
HTH
Regards![]()
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 ... JKOriginally Posted by Brainbug
.. suppose typo but, anyway you are wellcome for help ..anytime, if i am able to be of help ... Regards
![]()
I changed a little bit, so you can set any kind of text in the progress bar.
PrivateSub UpdateLabel(ByVal PB1 As ProgressBar, ByVal sInputString AsString, OptionalByVal oFont As Font = Nothing)
Dim sString AsString
Dim canvas As Graphics = PB1.CreateGraphics
Dim iWidth AsInteger
If IsNothing(oFont) Then
oFont = New Font("Verdana", 8, FontStyle.Bold)
EndIf
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()
EndSub
regrards
Nighty![]()
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
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.
Some useful links: Learning videoes, Code Samples, WMI Code Creator, MSDN, The Code Project, WindowsClient.net, ASP.net, W3 Schools, Regular-Expressions.info, GDI+ FAQ
How to format posts with code blocks etc - present the problem/post properly
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
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
You draw in Paint event and you use the provided graphics instance e.Graphics to draw with.
Some useful links: Learning videoes, Code Samples, WMI Code Creator, MSDN, The Code Project, WindowsClient.net, ASP.net, W3 Schools, Regular-Expressions.info, GDI+ FAQ
How to format posts with code blocks etc - present the problem/post properly
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![]()
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.
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
Some useful links: Learning videoes, Code Samples, WMI Code Creator, MSDN, The Code Project, WindowsClient.net, ASP.net, W3 Schools, Regular-Expressions.info, GDI+ FAQ
How to format posts with code blocks etc - present the problem/post properly
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 by [RTS]BN+VS*; 02-05-2009 at 3:23 PM.
Bookmarks