Click here to Skip to main content
15,881,172 members
Articles / Desktop Programming / Windows Forms

Progress Bar using C#

Rate me:
Please Sign up or sign in to vote.
3.25/5 (10 votes)
23 Sep 2010CPOL 191.3K   7.7K   8   20
Loading an image after the progress control completes its execution
Image 1

Image 2

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.

C#
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:

C#
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:

C#
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:

C#
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)


Written By
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.

Comments and Discussions

 
General[My vote of 1] Copy from MSDN Pin
Giri Ganji9-Feb-14 22:02
Giri Ganji9-Feb-14 22:02 
NewsCOPIED Pin
Divakar Raj M23-Jul-13 23:36
professionalDivakar Raj M23-Jul-13 23:36 
Questionc# Pin
Arvind100187-Mar-13 2:32
Arvind100187-Mar-13 2:32 
GeneralMy vote of 2 Pin
Gianluca Simionato23-Sep-10 20:19
Gianluca Simionato23-Sep-10 20:19 
GeneralMy vote of 2 Pin
Samuel Cragg23-Sep-10 7:05
Samuel Cragg23-Sep-10 7:05 
GeneralRe: My vote of 2 Pin
Drew Stainton23-Sep-10 9:05
Drew Stainton23-Sep-10 9:05 
GeneralRe: My vote of 2 Pin
Samuel Cragg23-Sep-10 9:38
Samuel Cragg23-Sep-10 9:38 
GeneralRe: My vote of 2 Pin
Drew Stainton23-Sep-10 9:52
Drew Stainton23-Sep-10 9:52 
GeneralMy vote of 1 Pin
karabax23-Sep-10 7:01
karabax23-Sep-10 7:01 
nothing new.
GeneralRe: My vote of 1 Pin
Drew Stainton23-Sep-10 9:04
Drew Stainton23-Sep-10 9:04 
GeneralRe: My vote of 1 Pin
karabax23-Sep-10 10:05
karabax23-Sep-10 10:05 
GeneralRe: My vote of 1 Pin
Drew Stainton23-Sep-10 10:31
Drew Stainton23-Sep-10 10:31 
GeneralRe: My vote of 1 Pin
karabax24-Sep-10 6:22
karabax24-Sep-10 6:22 
GeneralMy vote of 2 Pin
sam.hill23-Sep-10 5:54
sam.hill23-Sep-10 5:54 
GeneralDispose Pin
Luc Pattyn23-Sep-10 2:58
sitebuilderLuc Pattyn23-Sep-10 2:58 
GeneralRe: Dispose Pin
Syed Shabeer23-Sep-10 3:36
Syed Shabeer23-Sep-10 3:36 
GeneralWhy Pin
Trollslayer22-Sep-10 23:00
mentorTrollslayer22-Sep-10 23:00 
GeneralRe: Why Pin
Syed Shabeer22-Sep-10 23:06
Syed Shabeer22-Sep-10 23:06 
GeneralRe: Why Pin
Syed Shabeer22-Sep-10 23:08
Syed Shabeer22-Sep-10 23:08 
GeneralRe: Why Pin
Drew Stainton23-Sep-10 9:03
Drew Stainton23-Sep-10 9:03 

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

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