Can this be done?

My question is: how do I change the color of a progress bar By color I mean that intense green used to draw the blocks of the bar.

I found a property in .NET Framework 1.1 called ProgressBarColor that is supposed to do that, but I did not find it in .NET 2.0. Did the programmers overlook it on purpose or by mistake



Answer this question

Can this be done?

  • Henrique Ribeiro

    The ProgressBar class is declared as NotInheritable.  If you turn off visual styles then you do so for the entire application.  Just use a Panel, Label or PictureBox and use GDI+ to draw a coloured rectangle on it.

  • __sorcerer__

    I do not intend to use the ProgressBar for its usual purpose. I want to show something totaly different, and for that I need to be able to change its color. I implemented a control that does mainly that, but it looks bad compared to the ProgressBar.

    So, how do I turn off the visual styles for the application Or is there any smarter way If I inherit ProgressBar into a control I make, is there any way I can change the ForeColor if the visual styles are still turned on


  • jack zhao

    Many thanks Michel, I think I got it. I suspected from the beginning that the effort was not worth it, but I just wanted to ask if any1 know a shortcut. Anyways, your ideea with text clipping is not that hard to implement. Think I'll try it one of these days.

    I wonder if just 1 pixel will be enough precision though, prob will though.


  • Matt D

    The blocks are drawn using the ForeColor property however if visual styles are enabled for the app then the progress bar uses the settings from Windows irrelevant of what you set for the properties.

    Michael Taylor - 1/12/06


  • Fredrik Melin

    Oh Code Project, you've done it again. Added that one to my own bookmarks.

  • byeadon

    It is generally considered poor design to change the color. The color should change based on what theme the user is using in their system. If you are writing an interface where you need this to change, then I believe the above post is correct in that it uses the ForeColor.


  • neogortex

    That is what I have already done, but it doesn't look very pretty. After all, it is only a colored rectangle.

    Thanks for the answers & your time.


  • Jeff Williams

  • SarahB

    That's exactly what the ProgressBar looks like without visual styles.  What exactly is this other purpose for which you need to use this control anyway

  • Paul.Davis

    Well, I am writing a Pen&Paper RPG Game utility, and I want to show a unit's HP(Health Points) left in a rectangle, and color the rectangle in a certain color proportional to the HP_current / HP_total. I also need the color to change when HP_current goes above certain limits.

    Also, I need to do something else which I suspect is impossible using simple GDI+.

    I have a rectangle, which I partially color - let's say 60% of it is green, rest white. I want to write a string inside that rectangle. The problem is that I want the part of the string above the green part to have color1, and the part of the string abore the white part to have color2. Obviously, color1 != color2.

    Can this be done without going into manually writing strings pixel-by-pixel


  • Alquimista09

    Well that looks nice but it is not what I wanted.

    -------------------------------------------------
    |                          this is|the thing       |
    -------------------------------------------------
     

    Suppose that is the progress bar, and Delimiter := the "|" between "is" and "the".

    Left of Delimiter the bar is red, right it is white. I want to write "this is" with blue, and "the thing" with red. And the problem comes if the Delimiter cuts through a letter... Can that be done


  • Mchafu

    I agree. The effort probably isn't worth it but it probably isn't that bad to implement. Ideally what you want to do is XOR the text to the background color. I think this will cause the text to swap with the background. But of course you probably want the background to use 2 different colors and the text to use 2 different colors.

    Therefore your best option is to draw 2 separate rectangles inside your control. The first rectangle contains the percentage of the bar that should be colored and will have the text drawn using the appropriate color. By telling Windows to clip the text you'll get the text streaming off the side of the control. Then by doing the same thing with the second rectangle but using the unfilled color and the rest of the string you'll get the illusion of a continous bar with text.

    Of course the biggest issue will probably be getting the text to line up just right. I'd recommend that you actually only support changing the text color at character boundaries. Then I think lining up the text will be easier and nobody would notice. Alternatively you could draw the entire control as unfilled with the text using the unfilled color and then overlay a new rectangle with the appropriate filled color. If done right then it should be pretty hard to tell that the overlay is occurring. Of course there is the slight chance that rounding errors (when it comes to determining where to place the pixels for a letter) may cause slight glitches I doubt anybody would notice.

    Michael Taylor - 1/18/06


  • kwix

    Anything can be done, but it's a matter of whether it's worth the effort. What you're asking for is very specific. It would take a reasonable amount of code to custom draw the control. Unless you can find a ready made third-party option then you're in for a fun time doing it yourself. Why is it always those with the least experience who want the fanciest UIs

  • Can this be done?