Custom Progressbar

Experienced Elementalist
Joined
Jul 27, 2014
Messages
252
Reaction score
8
Hey guys
I'm trying to create a custom progress bar using this code
Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing.Drawing2D;


namespace Updater
{
    [CLSCompliant(true)]
    public partial class ProgressBar : UserControl
    {
        public ProgressBar()
        {
            InitializeComponent();
            SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
        }
        [CLSCompliant(true)]
        [RefreshProperties(RefreshProperties.Repaint)]
        [Browsable(true)]
        [Category("Appearance")]
        [DefaultValue(100)]
        public int Maximum {
            get {
                return _maximum;
            }
            set {
                _maximum = value;
                this.Invalidate();
            }
        }


        [CLSCompliant(true)]
        [RefreshProperties(RefreshProperties.Repaint)]
        [Browsable(true)]
        [Category("Appearance")]
        [DefaultValue(0)]
        public int Value {
            get {
                return _value;
            }
            set {
                _value = value;
                this.Invalidate();
            }
        }


        [CLSCompliant(true)]
        [RefreshProperties(RefreshProperties.Repaint)]
        [Browsable(true)]
        [Category("Appearance")]
        [DefaultValue(null)]
        public Image ProgressbarImage {
            get {
                return _progressbarImage;
            }
            set {
                _progressbarImage = value;
                this.Invalidate();
            }
        }


        [CLSCompliant(true)]
        [RefreshProperties(RefreshProperties.Repaint)]
        [Browsable(true)]
        [Category("Appearance")]
        [DefaultValue(typeof(Color), "DarkGray")]
        public Color ProgressbarColor {
            get {
                return _progressbarColor;
            }
            set {
                _progressbarColor = value;
                this.Invalidate();
            }
        }


        [CLSCompliant(true)]
        [Browsable(true)]
        [Category("Behavior")]
        [DefaultValue(true)]
        public bool RevealImage {
            get {
                return _revealImage;
            }
            set {
                _revealImage = value;
                this.Invalidate();
            }
        }


        private bool _revealImage = true;
        private int _maximum = 100;
        private int _value = 0;
        private Image _progressbarImage = null;
        private Color _progressbarColor = Color.DarkGray;


        protected override void OnPaint(System.Windows.Forms.PaintEventArgs e) 
        {


            Graphics g = e.Graphics;
            Rectangle r = this.ClientRectangle;


            r.Inflate(-1, -1);
            r.Width = (((r.Width * (_value / _maximum))) - 1);
            if ((r.Width < 1)) {
                return;
            }


            if (_progressbarImage == null) {
                using (SolidBrush b = new SolidBrush(_progressbarColor)) {
                    g.FillRectangle(b, r);
                }
            }
            else {
                g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                if (_revealImage) {
                    g.DrawImage(_progressbarImage, r, r, GraphicsUnit.Pixel);
                }
                else {
                    g.DrawImage(_progressbarImage, r);
                }
            }
        }
    }
}

This code can also be found
I'm having a problem though.
One I run the program, I try to test it with just a timer and a simple background fill
But it doesn't update the progress bar as it goes along.
It only updates it one it is completed.

I feel like the problem is occuring because of these lines
Code:
r.Inflate(-1, -1);
            r.Width = (((r.Width * (_value / _maximum))) - 1);
            if ((r.Width < 1)) {
                return;
            }

So I added a label to see if the value of the progress bar was actually changing, and it was.

So my question is what's stopping the progress bar from repainting for every value increase?
Hope someone can help out, thanks!