Click here to Skip to main content
Click here to Skip to main content

Progress Bar using C#

, 23 Sep 2010 CPOL
Rate this:
Please Sign up or sign in to vote.
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

License

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

Share

About the Author

Syed Shabeer
Software Developer
India India
A Software programmer. Works on ASP.NET, MVC, VB.NET, C#, .NET Compact framework, SSIS, SQL Server, PHP and its frameworks. Other than that likes to play games in PC.
Follow on   Twitter

Comments and Discussions

 
General[My vote of 1] Copy from MSDN PinmemberGiri Ganji9-Feb-14 23:02 
NewsCOPIED PinmemberDivakar Raj Mohan Kumar24-Jul-13 0:36 
Questionc# PinmemberArvind100187-Mar-13 3:32 
GeneralMy vote of 2 PinmemberGianluca Simionato23-Sep-10 21:19 
GeneralMy vote of 2 PinmemberSam Cragg23-Sep-10 8:05 
GeneralRe: My vote of 2 PinmemberDrew Stainton23-Sep-10 10:05 
GeneralRe: My vote of 2 PinmemberSam Cragg23-Sep-10 10:38 
GeneralRe: My vote of 2 PinmemberDrew Stainton23-Sep-10 10:52 
GeneralMy vote of 1 Pinmemberkarabax23-Sep-10 8:01 
GeneralRe: My vote of 1 PinmemberDrew Stainton23-Sep-10 10:04 
GeneralRe: My vote of 1 Pinmemberkarabax23-Sep-10 11:05 
GeneralRe: My vote of 1 PinmemberDrew Stainton23-Sep-10 11:31 
GeneralRe: My vote of 1 Pinmemberkarabax24-Sep-10 7:22 
GeneralMy vote of 2 Pinmembersam.hill23-Sep-10 6:54 
GeneralDispose PinmvpLuc Pattyn23-Sep-10 3:58 
GeneralRe: Dispose PinmemberSyed Shabeer23-Sep-10 4:36 
GeneralWhy PinmentorTrollslayer23-Sep-10 0:00 
GeneralRe: Why PinmemberSyed Shabeer23-Sep-10 0:06 
GeneralRe: Why PinmemberSyed Shabeer23-Sep-10 0:08 
GeneralRe: Why PinmemberDrew Stainton23-Sep-10 10:03 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150327.1 | Last Updated 23 Sep 2010
Article Copyright 2010 by Syed Shabeer
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid