How do I link one control with a component, say a progressbar linked to a custom TaskBar Component

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Ive learning C# for a while, for my project in college I created a custom library with many Win8, Win7 Native Controls.
And for the Taskbar component in my project, I want to link it with a Progress Bar – like if there are so many progress bars in a form adding this component we can select a progress bar to show the progress in Task Bar, also we can change it in runtime also.
but the thing is I have problem with the Event registering with the Progress Bar linking to it, Im not getting the progress change in Taskbar.
Here Im putting the TaskBar Component Code...
Since Im a Beginner, I dont know advanced debugging, Could anyone please ... help me out!!!
<pre class="prettyprint //This is Component SourceCode
//by Nirmal Guru
//AeroControls Library

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Drawing;
using System.Windows.Forms;

namespace AeroControls
{
/// <summary>
/// Control for setting up the TaskBar Progress Control.
/// </summary>
[ToolboxBitmap(typeof(System.Windows.Forms.ProgressBar))]
public partial class TaskBarProgress : Component
{
#region Initialization
ContainerControl owner;
private ProgressBar ProgressBarLink;
bool showInTaskbar;
private TaskBarState state = TaskBarState.Normal;
private ProgressBarState CurrentState = ProgressBarState.Normal;

/// <summary>
/// Creatyes A new instance of TaskBar Progress Control.
/// </summary>
public TaskBarProgress()
{
InitializeComponent();
}

/// <summary>
/// Creatyes A new instance of TaskBar Progress Control.
/// </summary>
/// <param name="containerControl Parent Container</param>
public TaskBarProgress(ContainerControl containerControl)
: this()
{
owner = containerControl;
InitializeComponent();
}
#endregion

#region ParentControl Propery
/// <summary>
/// Sets the Parent Control
/// </summary>
[Description("Gets or sets the Parent Control."), Category("Design")]
public ContainerControl ParentControl
{
get { return owner; }
set { owner = value; }
}

/// <summary>
/// Ensures Parent Control property is set
/// </summary>
public override ISite Site
{
set
{
// Runs at design time, ensures designer initializes ContainerControl
base.Site = value;
if (value == null) return;
IDesignerHost service = value.GetService(typeof(IDesignerHost)) as IDesignerHost;
if (service == null) return;
IComponent rootComponent = service.RootComponent;
ParentControl = rootComponent as ContainerControl;
}
}
#endregion

#region Properties
/// <summary>
/// Links the TaskProgress Control with available ProgressBar Control.
/// Only Single ProgressBar can be linked at a time.
/// </summary>
[Description("Gets or sets the Linker ProgressBar Control."), Category("Design")]
public ProgressBar ProgressBarControl
{
get { return ProgressBarLink; }
set
{
ProgressBarLink = value;
OnProgressBarLinkChanged(EventArgs.Empty);
}
}

/// <summary>
/// Show progress in taskbar for Windows 7 and above only.
/// </summary>
[Description("Gets or sets the Progress state in the TaskBar in Windows 7 and above."), Category("Behavior"), DefaultValue(true)]
public bool ShowInTaskbar
{
get { return showInTaskbar; }
set
{
if (showInTaskbar != value)
{
showInTaskbar = value;

// send signal to the taskbar.
if (owner != null)
{
ProgressBarLink.ValueChanged += new EventHandler<EventArgs>(ProgressBarLink_ValueChanged);
ProgressBarLink.StateChanged += new EventHandler<EventArgs>(ProgressBarLink_StateChanged);

if (ProgressBarLink.Style != ProgressBarStyle.Marquee)
{ SetValueInTB(); }

SetStateInTB();
}
}
}
}
#endregion

#region Events
// Declare an event
/// <summary>
/// Fires when the ProgressBarLink property is changed
/// </summary>
[Category("Action")]
[Description("Fires when the ProgressBarLink property is changed")]
public event EventHandler<EventArgs> ProgressBarLinkChanged;

/// <summary>
/// Raises the Event when the ProgressBarLink property changes
/// </summary>
/// <param name="e EventArgs</param>
protected virtual void OnProgressBarLinkChanged(EventArgs e)
{
// Raise the event
EventHandler<EventArgs> handler = this.ProgressBarLinkChanged;
if (handler != null)
handler(this, e);
}
#endregion

#region ProgressBar

void ProgressBarLink_ValueChanged(object sender, EventArgs e)
{
// send signal to the taskbar.
SetValueInTB();
}

/// <summary>
/// The taskprogressbar state for Windows Vista & 7.
/// </summary>
void ProgressBarLink_StateChanged(object sender, EventArgs e)
{
CurrentState = ProgressBarLink.State;

bool wasMarquee = ProgressBarLink.Style == ProgressBarStyle.Marquee;

if (wasMarquee)
// the Taskbar PB value needs to be reset
SetValueInTB();
else
// there wasnt a marquee, thus we need to update the taskbar
SetStateInTB();
}
#endregion

#region Methods
private void SetValueInTB()
{
if (showInTaskbar && owner != null)
{
ulong maximum = (ulong)(ProgressBarLink.Maximum - ProgressBarLink.Minimum);
ulong progress = (ulong)(ProgressBarLink.Value - ProgressBarLink.Minimum);

Taskbar.SetProgressValue(owner.Handle, progress, maximum);
}
}

private void SetStateInTB()
{
if (owner == null) return;

if (!showInTaskbar)
state = TaskBarState.NoProgress;
else if (ProgressBarLink.Style == ProgressBarStyle.Marquee)
state = TaskBarState.Indeterminate;
else if (CurrentState == ProgressBarState.Error)
state = TaskBarState.Error;
else if (CurrentState == ProgressBarState.Paused)
state = TaskBarState.Paused;

Taskbar.SetProgressState(owner.Handle, state);
}
#endregion
}
}[/code]
<br/>

View the full article
 
Back
Top