Click here to Skip to main content
15,897,891 members
Articles / Programming Languages / C# 4.0

EasyProgressBarPacket

Rate me:
Please Sign up or sign in to vote.
4.84/5 (42 votes)
18 Nov 2011CPOL4 min read 67.4K   6.9K   105  
A few progressbar examples with clone support.
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Windows.Forms.Design;
using System.ComponentModel;

namespace EasyProgressBarPacket
{
    /// <summary>
    /// Represents a custom progressbar control for all strip controls.
    /// </summary>
    [ToolboxBitmap(typeof(System.Windows.Forms.ProgressBar))]
    [ToolStripItemDesignerAvailability(ToolStripItemDesignerAvailability.All)]
    public class ToolStripEasyProgressBarItem : ToolStripControlHost, IProgressBar, ICloneable
    {
        #region Constructor

        /// <summary>
        /// Initializes a new instance of the ToolStripEasyProgressBarItem class. 
        /// </summary>
        public ToolStripEasyProgressBarItem()
            : base(CreateControlInstance())
        { }

        #endregion

        #region Destructor

        ~ToolStripEasyProgressBarItem()
        {
            GC.SuppressFinalize(this);
        }

        #endregion

        /// <summary>
        /// Gets the EasyProgressBar control that this ToolStripControlHost is hosting.
        /// </summary>
        [Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
        public EasyProgressBar ProgressBar
        {
            get { return base.Control as EasyProgressBar; }
        }

        #region Previous Hided Members

        /// <summary>
        /// Gets or sets the text displayed on the <see cref="ToolStripEasyProgressBarItem"/>.
        /// </summary>
        [Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public override string Text
        {
            get { return this.ProgressBar.Text; }
            set { this.ProgressBar.Text = value; }
        }

        /// <summary>
        /// Gets or sets the foreground color of the hosted control.
        /// </summary>
        [Category("Appearance")]
        [IsCloneable(true)]
        public override Color ForeColor
        {
            get { return this.ProgressBar.ForeColor; }
            set { this.ProgressBar.ForeColor = value; }
        }

        /// <summary>
        /// Gets or sets the font to be used on the hosted control.
        /// </summary>
        [Category("Appearance")]
        [IsCloneable(true)]
        public override Font Font
        {
            get { return this.ProgressBar.Font; }
            set { this.ProgressBar.Font = value; }
        }

        [Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public override Image BackgroundImage
        {
            get
            {
                return base.BackgroundImage;
            }
            set
            {
                base.BackgroundImage = value;
            }
        }

        [Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public override ImageLayout BackgroundImageLayout
        {
            get
            {
                return base.BackgroundImageLayout;
            }
            set
            {
                base.BackgroundImageLayout = value;
            }
        }

        /// <summary>
        /// Gets the height and width of the ToolStripEasyProgressBarItem in pixels.
        /// </summary>
        /// <value>
        /// Type: <see cref="System.Drawing.Size"/>
        /// A Point value representing the height and width.
        /// </value>
        protected override Size DefaultSize
        {
            get { return new Size(100, 20); }
        }

        /// <summary>
        /// Gets the spacing between the <see cref="ToolStripEasyProgressBarItem"/> and adjacent items.
        /// </summary>
        protected override Padding DefaultMargin
        {
            get
            {
                if ((base.Owner != null) && (base.Owner is StatusStrip))
                {
                    return new Padding(3, 3, 3, 3);
                }

                return new Padding(1, 1, 1, 2);
            }
        }

        #endregion

        #region Helper Methods

        private static Control CreateControlInstance()
        {
            return new EasyProgressBar();
        }

        private void OwnerRendererChanged(object sender, EventArgs e)
        {
            ToolStripRenderer toolsTripRenderer = base.Owner.Renderer;
            if (toolsTripRenderer != null)
            {
                if (toolsTripRenderer is ToolStripProfessionalRenderer)
                {
                    ToolStripProfessionalRenderer renderer = toolsTripRenderer as ToolStripProfessionalRenderer;
                    if (renderer != null)
                    {
                        this.BorderColor = renderer.ColorTable.ToolStripBorder;
                    }
                    if (base.Owner.GetType() != typeof(StatusStrip))
                    {
                        this.Margin = new Padding(1, 1, 1, 3);
                    }
                }
                else
                {
                    this.Margin = DefaultMargin;
                }
            }
        }

        #endregion

        #region Virtual Methods

        protected virtual void OnValueChanged()
        {
            if (ValueChanged != null)
                ValueChanged(this, EventArgs.Empty);
        }

        #endregion

        #region Override Methods

        /// <summary>
        /// Raises the OwnerChanged event. 
        /// </summary>
        /// <param name="e">An EventArgs that contains the event data.</param>
        protected override void OnOwnerChanged(EventArgs e)
        {
            base.OnOwnerChanged(e);

            if (base.Owner != null)
            {
                base.Owner.RendererChanged += new EventHandler(OwnerRendererChanged);
            }
        }

        #endregion

        #region IProgressBar Members

        [Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
        public GradientHover HoverGradient
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }
        private bool ShouldSerializeHoverGradient()
        {
            return false;
        }

        /// <summary>
        /// You can change the progress appearance from here.
        /// </summary>
        [Description("You can change the progress appearance from here")]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        [Browsable(true)]
        [Category("Gradient")]
        [IsCloneable(true)]
        public GradientProgress ProgressGradient
        {
            get { return this.ProgressBar.ProgressGradient; }
            set { this.ProgressBar.ProgressGradient = value; }
        }

        /// <summary>
        /// You can change the background appearance from here.
        /// </summary>
        [Description("You can change the background appearance from here")]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        [Browsable(true)]
        [Category("Gradient")]
        [IsCloneable(true)]
        public GradientBackground BackgroundGradient
        {
            get { return this.ProgressBar.BackgroundGradient; }
            set { this.ProgressBar.BackgroundGradient = value; }
        }

        /// <summary>
        /// You can change the color components of the progress bitmap[RGBA Colorizer for progress indicator].
        /// </summary>
        [Description("You can change the color components of the progress bitmap[RGBA Colorizer for progress indicator]")]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        [Browsable(true)]
        [Category("Gradient")]
        [IsCloneable(true)]
        public ColorizerProgress ProgressColorizer
        {
            get { return this.ProgressBar.ProgressColorizer; }
            set { this.ProgressBar.ProgressColorizer = value; }
        }

        /// <summary>
        /// You can change the background appearance of the DigitBox rectangle from here.
        /// </summary>
        [Description("You can change the background appearance of the DigitBox rectangle from here")]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        [Browsable(true)]
        [Category("Gradient")]
        [IsCloneable(true)]
        public GradientDigitBox DigitBoxGradient
        {
            get { return this.ProgressBar.DigitBoxGradient; }
            set { this.ProgressBar.DigitBoxGradient = value; }
        }

        /// <summary>
        /// Gets or Sets, the current progress value of the control.
        /// </summary>
        [Description("Gets or Sets, the current progress value of the control")]
        [DefaultValue(20)]
        [Browsable(true)]
        [Category("Progress")]
        [IsCloneable(true)]
        public int Value
        {
            get { return this.ProgressBar.Value; }
            set { this.ProgressBar.Value = value; OnValueChanged(); }
        }

        /// <summary>
        /// Gets or Sets, the minimum progress value of the control.
        /// </summary>
        [Description("Gets or Sets, the minimum progress value of the control")]
        [DefaultValue(0)]
        [Browsable(true)]
        [Category("Progress")]
        [IsCloneable(true)]
        public int Minimum
        {
            get { return this.ProgressBar.Minimum; }
            set { this.ProgressBar.Minimum = value; }
        }

        /// <summary>
        /// Gets or Sets, the maximum progress value of the control.
        /// </summary>
        [Description("Gets or Sets, the maximum progress value of the control")]
        [DefaultValue(100)]
        [Browsable(true)]
        [Category("Progress")]
        [IsCloneable(true)]
        public int Maximum
        {
            get { return this.ProgressBar.Maximum; }
            set { this.ProgressBar.Maximum = value; }
        }

        /// <summary>
        /// Determines whether the control's border is draw or not.
        /// </summary>
        [Description("Determines whether the control's border is draw or not")]
        [DefaultValue(true)]
        [Browsable(true)]
        [Category("Appearance")]
        [IsCloneable(true)]
        public bool IsPaintBorder
        {
            get { return this.ProgressBar.IsPaintBorder; }
            set { this.ProgressBar.IsPaintBorder = value; }
        }

        /// <summary>
        /// Determines whether the digital number drawing is enabled or not.
        /// </summary>
        [Description("Determines whether the digital number drawing is enabled or not")]
        [DefaultValue(false)]
        [Browsable(true)]
        [Category("Appearance")]
        [IsCloneable(true)]
        public bool IsDigitDrawEnabled
        {
            get { return this.ProgressBar.IsDigitDrawEnabled; }
            set { this.ProgressBar.IsDigitDrawEnabled = value; }
        }

        /// <summary>
        /// Determines whether the percentage text is show or hide.
        /// </summary>
        [Description("Determines whether the percentage text is show or hide")]
        [DefaultValue(true)]
        [Browsable(true)]
        [Category("Appearance")]
        [IsCloneable(true)]
        public bool ShowPercentage
        {
            get { return this.ProgressBar.ShowPercentage; }
            set { this.ProgressBar.ShowPercentage = value; }
        }

        /// <summary>
        /// Display text formatting for progressbar value.
        /// </summary>
        [Description("Display text formatting for progressbar value")]
        [DefaultValue("done")]
        [Browsable(true)]
        [Category("Appearance")]
        [IsCloneable(true)]
        public string DisplayFormat
        {
            get { return this.ProgressBar.DisplayFormat; }
            set { this.ProgressBar.DisplayFormat = value; }
        }

        /// <summary>
        /// Gets or Sets, the control's border color from here.
        /// </summary>
        [Description("Gets or Sets, the control's border color from here")]
        [DefaultValue(typeof(Color), "DarkGray")]
        [Browsable(true)]
        [Category("Appearance")]
        [IsCloneable(true)]
        public Color BorderColor
        {
            get { return this.ProgressBar.BorderColor; }
            set { this.ProgressBar.BorderColor = value; }
        }

        /// <summary>
        /// Gets or Sets, the current border style of the ProgressBar control.
        /// </summary>
        [Description("Gets or Sets, the current border style of the ProgressBar control")]
        [DefaultValue(typeof(EasyProgressBarBorderStyle), "Flat")]
        [Browsable(true)]
        [Category("Appearance")]
        [IsCloneable(true)]
        public EasyProgressBarBorderStyle ControlBorderStyle
        {
            get { return this.ProgressBar.ControlBorderStyle; }
            set { this.ProgressBar.ControlBorderStyle = value; }
        }

        /// <summary>
        /// Occurs when the progress value changed of the control.
        /// </summary>
        [Description("Occurs when the progress value changed of the control")]
        public event EventHandler ValueChanged;

        #endregion

        #region ICloneable Members

        public object Clone()
        {
            ToolStripEasyProgressBarItem toolStripItem = CustomControlsLogic.GetMyClone(this) as ToolStripEasyProgressBarItem;
            return toolStripItem;
        }

        #endregion
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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


Written By
Software Developer (Senior) ARELTEK
Turkey Turkey
Since 1998...

MCPD - Enterprise Application Developer

“Hesaplı hareket ettiğini zanneden ve onunla iftihar eyliyen dar kafalar; kurtulmağa, yükselmeğe elverişli hiç bir eser vücüda getirmezler. Kurtuluş ve yükselişi, ancak varlığına dayanan ve mülkü milletin gizli kapalı hazinelerini verimli hale getirmesini bilen, şahsi menfaatini millet menfaati uğruna feda eden, ruhu idealist, dimağı realist şahsiyetlerde aramalıdır.”

Nuri Demirağ, 1947

Comments and Discussions