Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#4.0

EasyProgressBarPacket

4.84/5 (42 votes)
18 Nov 2011CPOL4 min read 70.4K   6.9K  
A few progressbar examples with clone support.

Sample Image

Welcome screen

The library packet includes the following controls and components:

FloatWindowAlphaMakerComponent
AniEasyProgressTaskManagerComponent
AnimatedEasyProgressControlControl
EasyProgressBarSplashFormForm
EasyProgressBarControl

and includes three derived controls:

ToolStripEasyProgressBarItemderives from the ToolStripControlHost class
ToolStripAnimatedEasyProgressBarItemderives from the ToolStripControlHost class
EasyProgressBarColumnderives 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.

C#
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
KeysDescription
EndChanges to maximum value.
HomeChanges to minimum value.
LeftDecrease the current value on the control.
RightIncrease the current value on the control.
Return/EnterIt changes our control's docking mode.
F1Not 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:

C#
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.

C#
/// <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.

C#
/// <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.

PropertyDescription
FPSGets or sets the frame per second value of the specified image.
AnimatedGifGets or sets the image to be animated.
FrameCountGets the total number of frames of the specified image.
CurrentFrameGets the index of the active frame.
IsReverseEnabledDetermines whether the animated GIF should play backwards when it reaches the end of the frame.
IsWorkingIf the task monitor is running, return true, otherwise false.
CancelCheckIntervalHow often the thread checks to see if a cancellation message has been heeded (a number of seconds).
CancelWaitTimeHow 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.

C#
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:

C#
/// <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:

C#
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:

C#
/// <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)