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;
int max = 100;
int val = 0;
Color BarColor = Color.AliceBlue;
Graphics g;
SolidBrush brush;
protected override void OnResize(EventArgs e)
{
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;
rect.Width = (int)((float)rect.Width * percent);
g.FillRectangle(brush, rect);
Draw3DBorder(g);
}
public int Minimum
{
get
{
return min;
}
set
{
if (value < 0)
{
min = 0;
}
if (value > max)
{
min = value;
min = value;
}
if (val < min)
{
val = min;
}
this.Invalidate();
}
}
public int Maximum
{
get
{
return max;
}
set
{
if (value < min)
{
min = value;
}
max = value;
if (val > max)
{
val = max;
}
this.Invalidate();
}
}
public int Value
{
get
{
return val;
}
set
{
int oldValue = val;
if (value < min)
{
val = min;
}
else if (value > max)
{
val = max;
}
else
{
val = value;
}
float percent;
Rectangle newValueRect = this.ClientRectangle;
Rectangle oldValueRect = this.ClientRectangle;
percent = (float)(val - min) / (float)(max - min);
newValueRect.Width = (int)((float)newValueRect.Width * percent);
percent = (float)(oldValue - min) / (float)(max - min);
oldValueRect.Width = (int)((float)oldValueRect.Width * percent);
Rectangle updateRect = new Rectangle();
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;
this.Invalidate(updateRect);
}
}
public Color ProgressBarColor
{
get
{
return BarColor;
}
set
{
BarColor = value;
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)
{
this.progressBar1.Value++;
if (this.progressBar1.Value == 50)
{
formshow();
}
}
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";
Here i have given "blue hills" image
}
Build the solution and run it.
History
- Sep 23 2010 - Initial update