using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
namespace Wunnell.Windows.Forms
{
[ToolboxBitmap(typeof(ToolStripStatusLabel))]
public class ToolStripDateTimeStatusLabel : ToolStripStatusLabel
{
#region Constants
///
/// The default value for the CustomFormat property.
///
const string DEFAULT_CUSTOM_FORMAT = (string)null;
///
/// The default value for the Format property.
///
const DateTimePickerFormat DEFAULT_FORMAT = DateTimePickerFormat.Time;
#endregion // Constants
#region Variables
///
/// The custom format string used to format the date and/or time in the control.
///
private string _customFormat;
///
/// Determines whether dates and times are displayed using standard or custom formatting.
///
private DateTimePickerFormat _format;
#endregion // Variables
#region Properties
///
/// Gets or sets the custom date/time format string.
///
///
/// A string that represents the custom date/time format. The default is a null reference (Nothing in Visual Basic).
///
[
Category("Behavior"),
DefaultValue(DEFAULT_CUSTOM_FORMAT),
Description("The custom format string used to format the date and/or time in the control.")
]
public string CustomFormat
{
get
{
return this._customFormat;
}
set
{
if (value != this._customFormat)
{
this._customFormat = value;
this.UpdateText();
if (this._format == DateTimePickerFormat.Custom)
{
this.OnFormatChanged(EventArgs.Empty);
}
}
}
}
///
/// Gets or sets the format of the date and time displayed in the control.
///
///
/// One of the values. The default is Time.
///
[
Category("Appearance"),
DefaultValue(DEFAULT_FORMAT),
Description("Determines whether dates and times are displayed using standard or custom formatting.")
]
public DateTimePickerFormat Format
{
get
{
return this._format;
}
set
{
if (value != this._format)
{
this._format = value;
this.UpdateText();
this.OnFormatChanged(EventArgs.Empty);
}
}
}
#region Hidden Base Properties
///
/// Gets the color used to display an active link.
///
///
/// Always returns .
///
[
Bindable(false),
Browsable(false),
DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden),
EditorBrowsable(EditorBrowsableState.Never)
]
public new Color ActiveLinkColor
{
get { return Color.Empty; }
}
///
/// Gets a value indicating whether the ToolStripLabel is a hyperlink.
///
///
/// Always returns false.
///
[
Bindable(false),
Browsable(false),
DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden),
EditorBrowsable(EditorBrowsableState.Never)
]
public new bool IsLink
{
get
{
return false;
}
}
///
/// Gets a value that represents the behavior of a link
///
///
/// Always returns .
///
[
Bindable(false),
Browsable(false),
DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden),
EditorBrowsable(EditorBrowsableState.Never)
]
public new LinkBehavior LinkBehavior
{
get
{
return LinkBehavior.SystemDefault;
}
}
///
/// Gets the color used to display an active link.
///
///
/// Always returns .
///
[
Bindable(false),
Browsable(false),
DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden),
EditorBrowsable(EditorBrowsableState.Never)
]
public new Color LinkColor
{
get { return Color.Empty; }
}
///
/// Overridden. Gets the text that is to be displayed on the item.
///
[
Bindable(false),
Browsable(false),
DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden),
EditorBrowsable(EditorBrowsableState.Never)
]
public override string Text
{
get
{
return base.Text;
}
}
///
/// Gets the color used when displaying a link that that has been previously visited.
///
///
/// Always returns .
///
[
Bindable(false),
Browsable(false),
DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden),
EditorBrowsable(EditorBrowsableState.Never)
]
public new Color VisitedLinkColor
{
get { return Color.Empty; }
}
#endregion // Hidden Base Properties.
#endregion // Properties
#region Constructors
///
/// Initializes a new instance of the ToolStripDateTimeStatusLabel class.
///
///
/// This constructor sets the initial property value to Time.
///
public ToolStripDateTimeStatusLabel()
: base()
{
this.InitializeComponent();
// Set the default property values.
this._format = DEFAULT_FORMAT;
this._customFormat = DEFAULT_CUSTOM_FORMAT;
// Display the initial formatted date and time.
this.UpdateText();
}
#endregion // Constructors
#region Events
///
/// Occurs when the property value has changed or else the Format property value is
/// Custom and the property value has changed.
///
[Description("Occurs when the format used to display date and/or time values in the control has changed.")]
public event EventHandler FormatChanged;
#endregion // Events
#region Event Handlers
private void clock_Tick(object sender, EventArgs e)
{
this.UpdateText();
}
#endregion // Event Handlers
#region Methods
///
/// Gets a string representing the current date and/or time formatted in
/// accordance with the current Format and CustomFormat property values.
///
///
/// A formatted string representing the current date and/or time.
///
private string GetFormattedDateAndTime()
{
string formattedString;
switch (this._format)
{
case DateTimePickerFormat.Long:
formattedString = DateTime.Now.ToLongDateString();
break;
case DateTimePickerFormat.Short:
formattedString = DateTime.Now.ToShortDateString();
break;
case DateTimePickerFormat.Time:
formattedString = DateTime.Now.ToLongTimeString();
break;
case DateTimePickerFormat.Custom:
default:
formattedString = DateTime.Now.ToString(this._customFormat);
break;
}
return formattedString;
}
///
/// Raises the event.
///
///
/// An that contains the event data.
///
protected virtual void OnFormatChanged(EventArgs e)
{
if (this.FormatChanged != null)
{
this.FormatChanged(this, e);
}
}
///
/// Raises the event.
///
///
/// The original parent of the item.
///
///
/// The new parent of the item.
///
protected override void OnParentChanged(ToolStrip oldParent, ToolStrip newParent)
{
// Enable the timer if an only if the button is displayed in a run time form.
this.clock.Enabled = (!this.DesignMode && newParent != null);
if (this.clock.Enabled)
{
this.UpdateText();
}
base.OnParentChanged(oldParent, newParent);
}
///
/// Displays a formatted string representing the current date and/or time.
///
private void UpdateText()
{
base.Text = this.GetFormattedDateAndTime();
}
#endregion // Methods
#region Auto-generated Code
private Timer clock;
private IContainer components;
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.clock = new System.Windows.Forms.Timer(this.components);
//
// clock
//
this.clock.Interval = 1000;
this.clock.Tick += new System.EventHandler(this.clock_Tick);
}
#endregion // Auto-generated Code
}
}