Im trying to get a usercontrol to act as a progressbar. As far as i am aware, i need to draw a new bar ontop of the old one and increase its size according to the percentage completed. I have the following code, but unfortunately the green bar is at 100% even though i set the percentage property to 0% when the control is initialized. Im assuming i have made a dumb oversight but i cant see it, any help would be appreciated. Thanks.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Drawing2D;
namespace CustomPbar
{
public partial class Pbar : UserControl
{
private int PercentageValue;
public int Percentage
{
get { return PercentageValue;}
set
{
PercentageValue = value;
this.Invalidate();
}
}
public Pbar()
{
InitializeComponent();
Percentage = 0;
using(GraphicsPath path = new GraphicsPath())
{
path.StartFigure();
// top left arc
path.AddArc(0, 0, (10), (10), 180, 90);
//rect, 180, 90);
// top right arc
path.AddArc(((this.Width) - (10)), 0, (10), (10), 270, 90);
// bottom right arc
path.AddArc(((this.Width) - (10)), ((this.Height) - (10)), (10), (10), 0, 90);
// bottom left arc
path.AddArc(0, ((this.Height) - (10)), (10), (10), 90, 90);
path.CloseFigure();
this.Region = new Region(path);
this.BackColor = SystemColors.ControlLight;
this.BackgroundImage = new Bitmap(@"c:\users\FrazMan\Desktop\pb1.bmp");
this.BackgroundImageLayout = ImageLayout.Stretch;
}
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
Rectangle rect = new Rectangle(0, 0, ((this.Width)*((Percentage)/100)), this.Height);
using (GraphicsPath gp = new GraphicsPath())
{
gp.StartFigure();
// top left arc
gp.AddArc(0, 0, (10), (10), 180, 90);
// top right arc
gp.AddArc(((rect.Width) - (10)), 0, (10), (10), 270, 90);
// bottom right arc
gp.AddArc(((rect.Width) - (10)), ((rect.Height) - (10)), (10), (10), 0, 90);
// bottom left arc
gp.AddArc(0, ((rect.Height) - (10)), (10), (10), 90, 90);
gp.CloseFigure();
SolidBrush greenBrush = new SolidBrush(Color.Green);
e.Graphics.FillPath(greenBrush, gp);
greenBrush.Dispose();
}
using(Graphics Draw = this.CreateGraphics())
{
Draw.DrawString(Percentage.ToString() + "%", ProgressBar.DefaultFont, Brushes.Black, new PointF((this.Width / 2) - ((Draw.MeasureString(Percentage.ToString() + "%", ProgressBar.DefaultFont)).Width / 2.0F),
(this.Height / 2) - ((Draw.MeasureString(Percentage.ToString() + "%", ProgressBar.DefaultFont)).Height / 2.0F)));
}
}
protected override void OnResize(EventArgs e)
{
base.OnResize(e);
this.Refresh();
}
}
}
Several places you create a
Rectangle, but never use it. I think you want to use therectwidth and height instead ofthiswidth and height.You should also be using
e.Graphicsinstead ofthis.CreateGraphics()for drawing the percentage string.There is a large amount of duplicate code, and I recommend you keep all drawing code in
OnPaintand when you want to redraw, callthis.Refresh(). It will help a lot with maintenance.