Click here to Skip to main content
Click here to Skip to main content

EasyProgressBarPacket

, 18 Nov 2011
Rate this:
Please Sign up or sign in to vote.
A few progressbar examples with clone support.

Sample Image

Welcome screen

The library packet includes the following controls and components:

FloatWindowAlphaMaker Component
AniEasyProgressTaskManager Component
AnimatedEasyProgressControl Control
EasyProgressBarSplashForm Form
EasyProgressBar Control

and includes three derived controls:

ToolStripEasyProgressBarItem derives from the ToolStripControlHost class
ToolStripAnimatedEasyProgressBarItem derives from the ToolStripControlHost class
EasyProgressBarColumn derives from the DataGridViewColumn class

Introduction

This library has a few progressbar examples for strip controls, DataGridView columns, and a Windows Forms application with clone support.

  • RGBA changer
  • Docking support
  • Supports digital number style [7 segment]
  • Supports dynamic properties for end-users
  • Provides keyboard support to the user for progressbar operations

For more details, please look at my EasyProgressBar for Windows Forms Application article.

Background

Sample Image

EasyProgressBar sample

We need to create specific classes, properties, methods, and a progress event for our controls. To do this, I have implemented custom classes and defined an interface and its name is IProgressBar. EasyProgressBar and ToolStripEasyProgressBarItem implement this interface.

public interface IProgressBar
{
    /// </summary>
    /// You can change the hover cell gradient appearance from here.
    /// </summary>
    GradientHover HoverGradient { get; set; }
    
    /// </summary>
    /// You can change the progress appearance from here.
    /// </summary>
    GradientProgress ProgressGradient { get; set; }

    /// </summary>
    /// You can change the background appearance from here.
    /// </summary>
    GradientBackground BackgroundGradient { get; set; }

    /// </summary>
    /// You can change the color components of the progress
    /// bitmap[RGBA Colorizer for progress indicator].
    /// </summary>
    ColorizerProgress ProgressColorizer { get; set; }

    /// </summary>
    /// You can change the background appearance
    /// of the DigitBox rectangle from here.
    /// </summary>
    GradientDigitBox DigitBoxGradient { get; set; }

    /// <summary>
    /// Gets or Sets, the current progress value of the control.
    /// </summary>
    int Value { get; set; }

    /// <summary>
    /// Gets or Sets, the minimum progress value of the control.
    /// </summary>
    int Minimum { get; set; }

    /// <summary>
    /// Gets or Sets, the maximum progress value of the control.
    /// </summary>
    int Maximum { get; set; }

    /// <summary>
    /// Determines whether the control's border is draw or not.
    /// </summary>
    bool IsPaintBorder { get; set; }
    
    /// <summary>
    /// Determines whether the digital number drawing is enabled or not.
    /// </summary>
    bool IsDigitDrawEnabled { get; set; }

    /// <summary>
    /// Determines whether the percentage text is show or hide.
    /// </summary>
    bool ShowPercentage { get; set; }

    /// <summary>
    /// Display text formatting for progressbar value.
    /// </summary>
    string DisplayFormat { get; set; }

    /// <summary>
    /// Gets or Sets, the control's border color from here.
    /// </summary>
    Color BorderColor { get; set; }

    /// <summary>
    /// Gets or Sets, the current border style of the ProgressBar control.
    /// </summary>
    EasyProgressBarBorderStyle ControlBorderStyle { get; set; }

    /// <summary>
    /// Occurs when the progress value changed of the control.
    /// </summary>
    event EventHandler ValueChanged;
}

Shortcut Keys

If IsUserInteraction property is enabled, provides keyboard support to the user for EasyProgressBar operations.

Keyboard Keys
Keys Description
End Changes to maximum value.
Home Changes to minimum value.
Left Decrease the current value on the control.
Right Increase the current value on the control.
Return/Enter It changes our control's docking mode.
F1 Not implemented.

EasyProgressBarColumn

Sample Image

EasyProgressBarColumn sample

To create a progressbar column for DataGridView controls, you must first create a class that derives from the DataGridViewColumn class. You can then override the CellTemplate property in this class. Here's the class declaration:

public class EasyProgressBarColumn : DataGridViewColumn
{
    #region Constructor

    public EasyProgressBarColumn()
        : base(new EasyProgressBarCell())
    {
        // To do something.
    }

    #endregion
    
    #region Property
        
    /* Override CellTemplate Property */
    public override DataGridViewCell CellTemplate
    {
        get { return base.CellTemplate; }
        set
        {
            // Ensure that the cell used for the template is an EasyProgressBarCell.
            if (value != null &&
                !value.GetType().IsAssignableFrom(typeof(EasyProgressBarCell)))
            {
                throw new InvalidCastException(
                  "The cell template must be an EasyProgressBarCell.");
            }

            base.CellTemplate = value;
        }
    }

    #endregion
}

To use the percentage format or change the display format for your columns, you need to use the CellStyle Builder dialog.

Sample Image

CellStyle builder dialog

EasyProgressBar for strip controls

To create a progressbar for strip controls, you must first create a class that derives from the ToolStripControlHost class. The following code snippet shows how you can create a custom progressbar for all strip controls.

/// <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
{
    // To do something.
}

Sample Image

ToolStripEasyProgressBarItem sample

Animated progress control for strip controls

The following code snippet shows how you can create a custom animated progress control for all strip controls.

/// <summary>
/// Represents a custom animated progressbar control for all strip controls.
/// </summary>
[ToolboxBitmap(typeof(System.Windows.Forms.ProgressBar))]
[ToolStripItemDesignerAvailability(ToolStripItemDesignerAvailability.All)]
public class ToolStripAnimatedEasyProgressBarItem : ToolStripControlHost, ICloneable
{
    // To do something.
}

Sample Image

ToolStripAnimatedEasyProgressBarItem samples

AniEasyProgressTaskManager

We use this component to apply animation support for a progressbar image. The important properties for the component are listed in the below table.

Property Description
FPS Gets or sets the frame per second value of the specified image.
AnimatedGif Gets or sets the image to be animated.
FrameCount Gets the total number of frames of the specified image.
CurrentFrame Gets the index of the active frame.
IsReverseEnabled Determines whether the animated GIF should play backwards when it reaches the end of the frame.
IsWorking If the task monitor is running, return true, otherwise false.
CancelCheckInterval How often the thread checks to see if a cancellation message has been heeded (a number of seconds).
CancelWaitTime How long the thread will wait (in total) before aborting a thread that hasn't responded to the cancellation message. TimeSpan.Zero means polite stops are not enabled.
Properties from the AniEasyProgressTaskManager component

How can I use this component?

To use this tool for your AnimatedEasyProgressControl or EasyProgressBarSplashForm, firstly, you need to add a new AniEasyProgressTaskManager component to your design form and connect it with your animated control or splash form. And then, you can call the Start(Control invokeContext) and StopTask() methods of the component.

private void btnGetData_Click(object sender, EventArgs e)
{
    aniEasyProgressTaskManager1.Start(animatedEasyProgressControl1);
    
    // To do something.
    
    aniEasyProgressTaskManager1.StopTask();
}

You can also customize your progress bitmap while the task is running, or handle a new image when the index of the active frame changes. To do this, you can use FrameChanged event of the animated control or splash form.

Sample Image

Popup sample

Clone support

I've created an attribute to apply clone support to a specific property. This way, we can copy control properties that are important to us. After that, we need to link this attribute to the appropriate property. The following code snippet shows how you can add this attribute to your appropriate property declaration:

/// <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; set; }

You can do the same behavior for other properties. To clone a control, we call the below code in our method:

public object Clone()
{
    EasyProgressBar progressBar = 
      CustomControlsLogic.GetMyClone(this) as EasyProgressBar;
    // Set its some base class properties.
    progressBar.Font = this.Font;
    progressBar.ForeColor = this.ForeColor;

    return progressBar;
}

Here's the clone method and the details for control cloning:

/// <summary>
/// Creates a new instance of the specified
/// type using that type's allowed properties.
/// <summary>
public static Object GetMyClone(Object source)
{
    // Grab the type of the source instance.
    Type sourceType = source.GetType();

    // Firstly, we create a new instance using that type's default constructor.
    object newObject = Activator.CreateInstance(sourceType, false);

    foreach (System.Reflection.PropertyInfo pi in sourceType.GetProperties())
    {
        if (pi.CanWrite)
        {
            // Gets custom attribute for the current property item.
            IsCloneableAttribute attribute = GetAttributeForSpecifiedProperty(pi);
            if (attribute == null)
                continue;

            if (attribute.IsCloneable)
            {
                // We query if the property support the ICloneable interface.
                Type ICloneType = pi.PropertyType.GetInterface("ICloneable", true);
                if (ICloneType != null)
                {
                    // Getting the ICloneable interface from the object.
                    ICloneable IClone = (ICloneable)pi.GetValue(source, null);
                    if (IClone != null)
                    {
                        // We use the Clone() method to set the new value to the property.
                        pi.SetValue(newObject, IClone.Clone(), null);
                    }
                    else
                    {
                        // If return value is null, just set it null.
                        pi.SetValue(newObject, null, null);
                    }
                }
                else
                {
                    // If the property doesn't support the ICloneable interface then just set it.
                    pi.SetValue(newObject, pi.GetValue(source, null), null);
                }
            }
        }
    }

    return newObject;
}

History

  • November 18, 2011 - Updated
    • Created a new animated progress control for all strip controls and added a new example for this.
    • Fixed NullReferenceException
  • November 14, 2011 - Updated
    • Added splashscreen example
    • Fixed ObjectDisposedException
  • November 11, 2011 - First release

License

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

About the Author

Burak Ozdiken
Software Developer (Senior) Litera Grup
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

 
GeneralThanks Pinmemberduzongfang30-Mar-13 19:05 
GeneralThanks Pinmemberrevno12-Feb-13 7:44 
GeneralMy vote of 5 PinmemberMaster.Man198011-Dec-12 4:30 
GeneralRe: My vote of 5 PinmemberBurak Ozdiken11-Dec-12 13:29 
QuestionExcellent .. but small issue PinmemberMember 228177129-Aug-12 1:28 
AnswerRe: Excellent .. but small issue PinmemberBurak Özdiken29-Aug-12 2:42 
GeneralRe: Excellent .. but small issue PinmemberMember 228177129-Aug-12 4:20 
GeneralRe: Excellent .. but small issue PinmemberBurak Özdiken29-Aug-12 6:13 
GeneralRe: Excellent .. but small issue PinmemberMember 228177129-Aug-12 6:55 
Generalnice! Pinmembermiguelsanc8-Aug-12 15:19 
GeneralRe: nice! [modified] PinmemberBurak Özdiken8-Aug-12 16:19 
GeneralRe: nice! Pinmembermiguelsanc11-Aug-12 10:11 
GeneralRe: nice! PinmemberBurak Özdiken11-Aug-12 13:22 
GeneralRe: nice! Pinmembermiguelsanc11-Aug-12 13:38 
GeneralRe: nice! PinmemberBurak Özdiken11-Aug-12 13:52 
GeneralRe: nice! Pinmembermiguelsanc11-Aug-12 13:56 
QuestionHow to consolidate the control into single class file that can be added to project PinmemberMember 228177115-Jul-12 8:32 
AnswerRe: How to consolidate the control into single class file that can be added to project Pinmemberburak29916-Jul-12 2:56 
GeneralSimply Amazing PinmemberMember 869768719-May-12 17:23 
GeneralRe: Simply Amazing Pinmemberburak29920-May-12 8:57 
GeneralMy Vote of 5 PinmemberRaviRanjankr25-Nov-11 3:03 
GeneralRe: My Vote of 5 Pinmemberburak29925-Nov-11 12:53 
GeneralMy vote of 5 Pinmemberamirhosseinhashemzadeh25-Nov-11 2:53 
GeneralRe: My vote of 5 Pinmemberburak29925-Nov-11 12:52 
GeneralMy vote of 5 PinmemberHalil ibrahim Kalkan24-Nov-11 21:22 
GeneralRe: My vote of 5 Pinmemberburak29925-Nov-11 12:51 
GeneralVote 5 Pinmemberbabaycuoi14-Nov-11 14:48 
GeneralRe: Vote 5 Pinmemberburak29914-Nov-11 15:36 
GeneralMy vote of 5 Pinmemberyichangzyh13-Nov-11 23:09 
GeneralRe: My vote of 5 Pinmemberburak29914-Nov-11 1:31 
GeneralMy vote of 5 PinmemberSergio Andrés Gutiérrez Rojas12-Nov-11 15:36 
GeneralRe: My vote of 5 Pinmemberburak29913-Nov-11 2:38 
GeneralMy vote of 5 PinmemberChrisKMPP11-Nov-11 22:39 
GeneralRe: My vote of 5 Pinmemberburak29912-Nov-11 1:20 
QuestionGood popup. PinmemberJefis11-Nov-11 8:21 
AnswerRe: Good popup. Pinmemberburak29912-Nov-11 1:17 

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

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

| Advertise | Privacy | Mobile
Web02 | 2.8.140709.1 | Last Updated 18 Nov 2011
Article Copyright 2011 by Burak Ozdiken
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid