Click here to Skip to main content
15,867,488 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 66.3K   6.9K   105   48
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)


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

 
QuestionProgressBar and Tasks TPL Pin
kiquenet.com2-Apr-17 23:44
professionalkiquenet.com2-Apr-17 23:44 
GeneralThanks Pin
duzongfang30-Mar-13 19:05
duzongfang30-Mar-13 19:05 
GeneralThanks Pin
revno12-Feb-13 7:44
revno12-Feb-13 7:44 
GeneralMy vote of 5 Pin
Master.Man198011-Dec-12 4:30
Master.Man198011-Dec-12 4:30 
GeneralRe: My vote of 5 Pin
Burak Ozdiken11-Dec-12 13:29
Burak Ozdiken11-Dec-12 13:29 
QuestionExcellent .. but small issue Pin
Member 228177129-Aug-12 1:28
Member 228177129-Aug-12 1:28 
AnswerRe: Excellent .. but small issue Pin
Burak Ozdiken29-Aug-12 2:42
Burak Ozdiken29-Aug-12 2:42 
GeneralRe: Excellent .. but small issue Pin
Member 228177129-Aug-12 4:20
Member 228177129-Aug-12 4:20 
GeneralRe: Excellent .. but small issue Pin
Burak Ozdiken29-Aug-12 6:13
Burak Ozdiken29-Aug-12 6:13 
GeneralRe: Excellent .. but small issue Pin
Member 228177129-Aug-12 6:55
Member 228177129-Aug-12 6:55 
Generalnice! Pin
miguelsanc8-Aug-12 15:19
miguelsanc8-Aug-12 15:19 
GeneralRe: nice! Pin
Burak Ozdiken8-Aug-12 16:19
Burak Ozdiken8-Aug-12 16:19 
GeneralRe: nice! Pin
miguelsanc11-Aug-12 10:11
miguelsanc11-Aug-12 10:11 
GeneralRe: nice! Pin
Burak Ozdiken11-Aug-12 13:22
Burak Ozdiken11-Aug-12 13:22 
nothing happens, Were you help me, maybe I could understand the problem.
Regards...
Burak Ozdiken

GeneralRe: nice! Pin
miguelsanc11-Aug-12 13:38
miguelsanc11-Aug-12 13:38 
GeneralRe: nice! Pin
Burak Ozdiken11-Aug-12 13:52
Burak Ozdiken11-Aug-12 13:52 
GeneralRe: nice! Pin
miguelsanc11-Aug-12 13:56
miguelsanc11-Aug-12 13:56 
QuestionHow to consolidate the control into single class file that can be added to project Pin
Member 228177115-Jul-12 8:32
Member 228177115-Jul-12 8:32 
AnswerRe: How to consolidate the control into single class file that can be added to project Pin
Burak Ozdiken16-Jul-12 2:56
Burak Ozdiken16-Jul-12 2:56 
GeneralSimply Amazing Pin
Member 869768719-May-12 17:23
Member 869768719-May-12 17:23 
GeneralRe: Simply Amazing Pin
Burak Ozdiken20-May-12 8:57
Burak Ozdiken20-May-12 8:57 
GeneralMy Vote of 5 Pin
RaviRanjanKr25-Nov-11 3:03
professionalRaviRanjanKr25-Nov-11 3:03 
GeneralRe: My Vote of 5 Pin
Burak Ozdiken25-Nov-11 12:53
Burak Ozdiken25-Nov-11 12:53 
GeneralMy vote of 5 Pin
AmirHashemZadeh25-Nov-11 2:53
professionalAmirHashemZadeh25-Nov-11 2:53 
GeneralRe: My vote of 5 Pin
Burak Ozdiken25-Nov-11 12:52
Burak Ozdiken25-Nov-11 12:52 

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

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