Click here to Skip to main content
15,899,679 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I tried too much to change the color of my ProgressBar, but it does not works at all. Does anyone have any idea?


Posted
Updated 27-Feb-10 22:11pm
v2

It's not a single color, as you can see. A progress bar is so simple that I'd suggest you're better off creating your own control, rather than owner drawing it, although I guess a control derived from a progress bar would be easier to drop into place of existing ones.
 
Share this answer
 
This link[^] creates a progress bar in visual basic. You've asked this question in C#, but this might get you started.
 
Share this answer
 
Thanks very much,
It was a grate link.
Although I know vb fair, but I could change the program in C#.

Also I changed these two lines because of some problems. I don't know if it was a right work or not, but it solved a problem when the control is running.


//this.Invalidate(updateRect); removed by editor<br />
this.Invalidate();             //added by editor<br />



codes in C#:
private int min = 0;//Minimum value for progress range
private int max = 100;// Maximum value for progress range
private int val = 0;// Current progress
private Color barColor = Color.Blue;// Color of progress thister

protected override void OnResize(EventArgs e)
{
    // Invalidate the control to get a repaint.
    this.Invalidate();
}

protected override void OnPaint(PaintEventArgs e)
{
    Graphics g = e.Graphics;
    SolidBrush brush = new SolidBrush(barColor);
    float percent = ((float)val - min) / (max - min);
    Rectangle rect = this.ClientRectangle;

    // Calculate area for drawing the progress.
    rect.Width = (int)(rect.Width * percent);

    // Draw the progress thister.
    g.FillRectangle(brush, rect);

    // Draw a three-dithisnsional border around the control.
    Draw3DBorder(g);

    // Clean up.
    brush.Dispose();
    g.Dispose();
}

public int Minimum
{
    get
    {
        return min;
    }
    set
    {
        // Prevent a negative value.
        if (value < 0)
            min = 0;

        // Make sure that the minimum value is never set higher than the maximum value.
        if (value > max)
        {
            min = value;
        }

        // Make sure that the value is still in range.
        if (val < min)
            val = min;

        // Invalidate the control to get a repaint.
        this.Invalidate();
    }
}

public int Maximum
{
    get
    {
        return max;
    }

    set
    {
        // Make sure that the maximum value is never set lower than the minimum value.
        if (value < min)
            min = value;

        max = value;

        // Make sure that the value is still in range.
        if (val > max)
            val = max;

        // Invalidate the control to get a repaint.
        this.Invalidate();
    }
}

public int Value
{
    get
    {
        return val;
    }

    set
    {
        int oldvalue = val;

        // Make sure that the value does not stray outside the valid range.
        if (value < min)
            val = min;
        else if (value > max)
            val = max;
        else
            val = value;

        // Invalidate only the changed area.
        float percent;

        Rectangle newvalueRect = this.ClientRectangle;
        Rectangle oldvalueRect = this.ClientRectangle;

        // Use a new value to calculate the rectangle for progress.
        percent = (val - min) / (max - min);
        newvalueRect.Width = (int)(newvalueRect.Width * percent);

        // Use an old value to calculate the rectangle for progress.
        percent = (oldvalue - min) / (max - min);
        oldvalueRect.Width = (int)(oldvalueRect.Width * percent);

        Rectangle updateRect = new Rectangle();

        // Find only the part of the screen that must be updated.
        if (newvalueRect.Width > oldvalueRect.Width)
        {
            updateRect.X = oldvalueRect.Size.Width;
            updateRect.Width = newvalueRect.Width - oldvalueRect.Width;
        }
        else
        {
            updateRect.X = newvalueRect.Size.Width;
            updateRect.Width = oldvalueRect.Width - newvalueRect.Width;
        }

        updateRect.Height = this.Height;
        // Invalidate only the intersection region.

        //this.Invalidate(updateRect); removed by editor
        this.Invalidate();             //added by editor
    }
}

public Color ProgressBarColor
{
    get
    {
        return barColor;
    }
    set
    {
        barColor = value;

        // Invalidate the control to get a repaint.
        this.Invalidate();
    }
}

private void Draw3DBorder(Graphics g)
{
    int PenWidth = (int)Pens.White.Width;

    g.DrawLine(Pens.DarkGray,
        new Point(this.ClientRectangle.Left, this.ClientRectangle.Top),
        new Point(this.ClientRectangle.Width - PenWidth, this.ClientRectangle.Top));
    g.DrawLine(Pens.DarkGray,
        new Point(this.ClientRectangle.Left, this.ClientRectangle.Top),
        new Point(this.ClientRectangle.Left, this.ClientRectangle.Height - PenWidth));
    g.DrawLine(Pens.White,
        new Point(this.ClientRectangle.Left, this.ClientRectangle.Height - PenWidth),
        new Point(this.ClientRectangle.Width - PenWidth, this.ClientRectangle.Height - PenWidth));
    g.DrawLine(Pens.White,
        new Point(this.ClientRectangle.Width - PenWidth, this.ClientRectangle.Top),
        new Point(this.ClientRectangle.Width - PenWidth, this.ClientRectangle.Height - PenWidth));
}
 
Share this answer
 
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900