I'd like to do two things on my progress bar.
- Change the green colour to red.
- 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.
I'd like to do two things on my progress bar.
Any information about those two things I wonder how to accomplish will be greatfuly appreaciated!
Thanks.
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
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
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;
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;
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
try using messsage PBM_SETBARCOLOR, that should do the trick with SendMessage
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);
}
}
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
I suggest you to take also a look to this article on CodeProject: Progress-O-Doom. It's great!
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