65.9K
CodeProject is changing. Read more.
Home

Progress Bar using C#

starIconstarIconstarIcon
emptyStarIcon
starIcon
emptyStarIcon

3.25/5 (10 votes)

Sep 23, 2010

CPOL
viewsIcon

193315

downloadIcon

7753

Loading an image after the progress control completes its execution

Introduction

This article shows the progress bar control loading animation and after the loading completes, an image loads in the form from the path specified. I found that in the similar way, the progress control can be used for the "Loading" process in many other cases. It is used in Winforms.

Using the Code

The controls needed to be added are Timer, Progress bar and a picture box.

The below code should be added actually after the form generated code in the Form Designer class file.

int min = 0;	// Minimum value for progress range
int max = 100;	// Maximum value for progress range
int val = 0;		// Current progress
Color BarColor = Color.AliceBlue;  // Color of progress meter
Graphics g;
SolidBrush brush;

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

protected override void OnPaint(PaintEventArgs e)
{
    g = e.Graphics;
    brush = new SolidBrush(BarColor);
    float percent = (float)(val - min) / (float)(max - min);
    Rectangle rect = this.ClientRectangle;
    
    // Calculate area for drawing the progress.
    rect.Width = (int)((float)rect.Width * percent);
    
    // Draw the progress meter.
    g.FillRectangle(brush, rect);
    
    // Draw a three-dimensional border around the control.
    Draw3DBorder(g);
}

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;
            min = value;
        }
        
        // Ensure 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 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 = (float)(val - min) / (float)(max - min);
        newValueRect.Width = (int)((float)newValueRect.Width * percent);
        
        // Use an old value to calculate the rectangle for progress.
        percent = (float)(oldValue - min) / (float)(max - min);
        oldValueRect.Width = (int)((float)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 the intersection region only.
        this.Invalidate(updateRect);
    }
}

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.AliceBlue,
        new Point(this.ClientRectangle.Left, this.ClientRectangle.Top),
        new Point(this.ClientRectangle.Width - PenWidth, this.ClientRectangle.Top));
    g.DrawLine(Pens.AliceBlue,
        new Point(this.ClientRectangle.Left, this.ClientRectangle.Top),
        new Point(this.ClientRectangle.Left, this.ClientRectangle.Height - PenWidth));
    g.DrawLine(Pens.Aqua,
        new Point(this.ClientRectangle.Left, this.ClientRectangle.Height - PenWidth),
        new Point(this.ClientRectangle.Width - PenWidth, 
		this.ClientRectangle.Height - PenWidth));
    g.DrawLine(Pens.Aqua,
        new Point(this.ClientRectangle.Width - PenWidth, this.ClientRectangle.Top),
        new Point(this.ClientRectangle.Width - PenWidth, 
			this.ClientRectangle.Height - PenWidth));
} 
//

Also add this below code in the button click event of a button in the window form:

private void button1_Click(object sender, EventArgs e)
{
     this.progressBar1.Value = 0;

     this.timer1.Interval = 100;
     this.timer1.Enabled = true;
} 

And also the below code in timer tick event:

if (this.progressBar1.Value < 50) // Should be less than progress bar max value
{
	this.progressBar1.Value++; 
	if (this.progressBar1.Value == 50) //The maximum value of the progress bar
	{
		formshow(); // Image loads when reached the max value
	}
}
else
{
	this.timer1.Enabled = false;
} 

Code for image load after the completion of progress bar control animation:

private void formshow()
{
	pictureBox1.ImageLocation = Application.StartupPath+ 
		"\\Blue hills.jpg"; // Put an image in /bin/debug area . 
		Here i have given "blue hills" image
}

Build the solution and run it.

History

  • Sep 23 2010 - Initial update