65.9K
CodeProject is changing. Read more.
Home

EasyProgressBarPacket

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.84/5 (42 votes)

Nov 11, 2011

CPOL

4 min read

viewsIcon

70356

downloadIcon

6921

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