views:

11209

answers:

11

I'd like to do two things on my progress bar.

  1. Change the green colour to red.
  2. Remove the blocks and make it in one color.

Any information about those two things I wonder how to accomplish will be greatfuly appreaciated!

Thanks.

A: 

Simply set the color property.

Mark Cooper
I did,its staying green.
John
+8  A: 

In the designer, you just need to set the ForeColor property to whatever color you'd like. In the case of Red, there's a predefined color for it.

To do it in code (C#) do this:

pgs.ForeColor = Color.Red;

Edit: Oh yeah, also set the Style to continuous. In code, like this:

pgs.Style = System.Windows.Forms.ProgressBarStyle.Continuous;

Another Edit: You'll also need to remove the line that reads Application.EnableVisualStyles() from your Program.cs (or similar). If you can't do this because you want the rest of the application to have visual styles, then I'd suggest painting the control yourself or moving on to WPF since this kind of thing is easy with WPF. You can find a tutorial on owner drawing a progress bar on codeplex

dustyburwell
No,its still green.
John
Updated with missing info.
dustyburwell
Forecolor=red ; Backcolor=blue; Style = Continious. Nothing changed,its staying like i never touched it
John
bah...okay, I see the problem. You need to disable visual styles in order for this to work with the properties. It's probably painting using whatever theme you have selected. In Program.cs there's a line that reads Application.EnableVisualStyles(). Remove that line and this will work. Otherwise, you'll need to paint the control yourself. The other option is to use WPF (if it's a possibility), since it lets you do this kind of stuff really easily.
dustyburwell
Please give me more information about "How to paint it by myself".
John
Here's a tutorial on owner drawing a progress bar. The process is much too complicated for a comment. http://www.codeproject.com/KB/cpp/VistaProgressBar.aspx
dustyburwell
+2  A: 

EDIT

By the sounds of things you're using the XP Theme which has the green block based prog-bar. Try flipping your UI Style to Windows Classic and test again, but you may need to implement your own OnPaint event to get it to do what you want across all UI Styles

Or as someone else pointed out, disable the VisualStyles for your application.

Original

As far as I know, the rendering of the Progress bar happens inline with the windows theme style that you've chosen (win2K, xp, vista)

You can change the color by setting the property

ProgressBar.ForeColor

I'm not sure that you can do much more however...

does some googling

Theres an article here from MS KB on creating a "Smooth" progress bar

http://support.microsoft.com/kb/323116

Eoin Campbell
@Eion,Forecolor=red ; Backcolor=blue; Style = Continious. Nothing changed,its staying like i never touched it.
John
A: 

Edit: in the two minuites it took me to fire up vs and check the syntax i was beaten to it with much better responses. i love this site.

        progressBar1 = new ProgressBar();
        progressBar1.ForeColor = Color.Red;
Fusspawn
Doesn't work.It stays empty now,i added progressBar1.value = 50,but still empty
John
tried setting the maxiumum value ? progressBar1 = new ProgressBar(); progressBar1.ForeColor = Color.Red; progressBar1.Maximum = 100;
Fusspawn
No,the problem is in my windows theme.
John
No idea if this will work, but try commenting out Application.EnableVisualThemes(); that is in one of the prebuilt files vs generate's ( I cant recall what one )
Fusspawn
Application.EnableVisualStyles() sorry not themes
Fusspawn
+2  A: 

Usually the progress bar is either themed or honors the user's color preferences. So for changing the color you either need to turn off visual styles and set ForeColor or draw the control yourself.

As for the continuous style instead of blocks you can set the Style property:

pBar.Style = ProgressBarStyle.Continuous;
Joey
+13  A: 

Since the previous answers don't appear to work in with Visual Styles. You'll probably need to create your own class or extend the progress bar:

public class NewProgressBar : ProgressBar
{
    public NewProgressBar()
    {
        this.SetStyle(ControlStyles.UserPaint, true);
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        Rectangle rec = e.ClipRectangle;

        rec.Width = (int)(rec.Width * ((double)Value / Maximum)) - 4;
        if(ProgressBarRenderer.IsSupported)
           ProgressBarRenderer.DrawHorizontalBar(e.Graphics, e.ClipRectangle);
        rec.Height = rec.Height - 4;
        e.Graphics.FillRectangle(Brushes.Red, 2, 2, rec.Width, rec.Height);
    }
}

EDIT: Updated code to make the progress bar use the visual style for the background

Chris Persichetti
Thanks for this, it was something I could work with in my solution.
Irwin
That's really cool, thanks. It really helped me out on something I'm working on at the moment, though I changed it to repaint the whole control each time instead of just the area specified by e.ClipRectangle. Otherwise, it didn't repaint correctly after only part of the control was invalidated by another window or the edge of the screen.
Matt Blaine
@Matt Blaine: Could you please post your modification as an edit to the answer? I believe there will be enough people interested in your complete solution.
Marek
@Marek Just noticed your comment. I don't have enough rep to edit the answer, so I posted my own. Thanks. http://stackoverflow.com/questions/778678/how-to-change-the-color-of-progressbar-in-c-net-3-5/2498036#2498036
Matt Blaine
A: 

try using messsage PBM_SETBARCOLOR, that should do the trick with SendMessage

Aleksandar
+3  A: 

Modificaton to dustyburwell's answer. (I don't have enough rep to edit it myself.) Like his answer, it works with "Visual Styles" enabled. You can just set the progressbar's ForeColor property in whatever form's design view.

using System;
using System.Windows.Forms;
using System.Drawing;

public class ProgressBarEx : ProgressBar
{
    private SolidBrush brush = null;

    public ProgressBarEx()
    {
        this.SetStyle(ControlStyles.UserPaint, true);
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        if (brush == null || brush.Color != this.ForeColor)
            brush = new SolidBrush(this.ForeColor);

        Rectangle rec = new Rectangle(0, 0, this.Width, this.Height);
        if (ProgressBarRenderer.IsSupported)
            ProgressBarRenderer.DrawHorizontalBar(e.Graphics, rec);
        rec.Width = (int)(rec.Width * ((double)Value / Maximum)) - 4;
        rec.Height = rec.Height - 4;
        e.Graphics.FillRectangle(brush, 2, 2, rec.Width, rec.Height);
    }
}
Matt Blaine
This is a great answer, worked well for me.
Kin
A: 

Hi,

I just put this into a static class.

  const int WM_USER = 0x400;
  const int PBM_SETSTATE = WM_USER + 16;
  const int PBM_GETSTATE = WM_USER + 17;

  [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)]
  static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);

  public enum ProgressBarStateEnum : int
  {
   Normal = 1,
   Error = 2,
   Paused = 3,
  }

  public static ProgressBarStateEnum GetState(this ProgressBar pBar)
  {
   return (ProgressBarStateEnum)(int)SendMessage(pBar.Handle, PBM_GETSTATE, IntPtr.Zero, IntPtr.Zero);
  }

  public static void SetState(this ProgressBar pBar, ProgressBarStateEnum state)
  {
   SendMessage(pBar.Handle, PBM_SETSTATE, (IntPtr)state, IntPtr.Zero);
  } 

Hope it helps,

Marc

Marc
A: 

I suggest you to take also a look to this article on CodeProject: Progress-O-Doom. It's great!

marco.ragogna
A: 

It works, BUT I cant access my designer, now :( To prevent possible data loss before loading the designer, the following errors must be resolved: An error occurred while parsing EntityName. Line 2, position 97

Dominik