using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace MarqueeLabel
{
/// <summary>
/// Scolls text across the screen.
/// </summary>
public partial class MarqueeLabel : UserControl
{
#region Types
/// <summary>
/// Defines constants that indicate the direction the text scolls across the screen.
/// </summary>
public enum MarqueeDirection
{
/// <summary>
/// Text scrolls to the left, reappearing at the right edge of the control.
/// </summary>
Left,
/// <summary>
/// Text scrolls to the right, reappearing at the left edge of the control.
/// </summary>
Right
}
#endregion // Types
#region Variables
/// <summary>
/// The direction the text scrolls across the screen.
/// </summary>
private MarqueeDirection _direction;
/// <summary>
/// The distance the text moves at each time interval.
/// </summary>
private int _distanceInterval = 5;
#endregion // Variables
#region Properties
/// <summary>
/// The direction the text scrolls across the screen.
/// </summary>
/// <value>
/// <see cref="MarqueeDirection">Left</see> if the text scrolls to the left; <see cref="MarqueeDirection">Right</see> if the text scrolls to the right.
/// </value>
[Category("Marquee")]
[Description("The direction the text moves.")]
public MarqueeDirection Direction
{
get { return this._direction; }
set { this._direction = value; }
}
/// <summary>
/// The distance the text moves each time interval.
/// </summary>
/// <value>
/// An <b>Int32</b> containing the number pixels the text will move.
/// </value>
/// <remarks>
/// The smaller the value the smoother the effect but the slower the text will move across the screen.
/// </remarks>
[Category("Marquee")]
[DefaultValue(5)]
[Description("The number of pixels the text moves each time.")]
public int DistanceInterval
{
get
{
return this._distanceInterval;
}
set
{
this._distanceInterval = value;
}
}
/// <summary>
/// The font used to display the text.
/// </summary>
/// <value>
/// A <see cref="Font"/> object.
/// </value>
[Category("Marquee")]
[Description("The font face of the text.")]
public Font MarqueeFont
{
get
{
return this.textLabel.Font;
}
set
{
this.textLabel.Font = value;
}
}
/// <summary>
/// Indicates whether the text will scroll or not.
/// </summary>
/// <value>
/// <b>True</b> if the text will scroll; otherwise, <b>False</b>.
/// </value>
[Category("Marquee")]
[DefaultValue(false)]
[Description("Whether or not the text moves.")]
public bool MarqueeEnabled
{
get
{
return this.marqueeTimer.Enabled;
}
set
{
this.marqueeTimer.Enabled = value;
if (!value)
{
this.textLabel.Left = 0;
}
}
}
/// <summary>
/// The text displayed on the label.
/// </summary>
/// <value>
/// A <b>String</b> containing the text to display.
/// </value>
[Category("Marquee")]
[Description("The text displayed on the control.")]
public string MarqueeText
{
get
{
return this.textLabel.Text;
}
set
{
this.textLabel.Text = value;
}
}
/// <summary>
/// The colour used to display the text.
/// </summary>
/// <value>
/// A <see cref="Color"/> object.
/// </value>
[Category("Marquee")]
[Description("The colour of the text.")]
public Color TextColour
{
get
{
return this.textLabel.ForeColor;
}
set
{
this.textLabel.ForeColor = value;
}
}
/// <summary>
/// The time interval between movements of the text.
/// </summary>
/// <value>
/// An <b>Int32</b> containing a number of milliseconds.
/// </value>
/// <remarks>
/// The smaller the value the faster the text will move.
/// </remarks>
[Category("Marquee")]
[DefaultValue(100)]
[Description("The number of milliseconds between text movements.")]
public int TimeInterval
{
get
{
return this.marqueeTimer.Interval;
}
set
{
this.marqueeTimer.Interval = value;
}
}
#endregion // Properties
#region Constructors
public MarqueeLabel()
{
InitializeComponent();
}
#endregion // Constructors
#region Event Handlers
private void marqueeTimer_Tick(object sender, EventArgs e)
{
switch (this._direction)
{
case MarqueeDirection.Left:
this.MoveLabelLeft();
break;
case MarqueeDirection.Right:
this.MoveLabelRight();
break;
}
}
#endregion // Event Handlers
#region Methods
/// <summary>
/// Moves the text to the left by the current distance interval.
/// </summary>
/// <remarks>
/// If the text moves beyond the visible area of the control it wraps back to the right edge.
/// </remarks>
private void MoveLabelLeft()
{
this.textLabel.Left -= this._distanceInterval;
if (this.textLabel.Right <= 0)
{
// Wrap the text back to the right edge of the control.
this.textLabel.Left = this.Width;
}
}
/// <summary>
/// Moves the text to the right by the current distance interval.
/// </summary>
/// <remarks>
/// If the text moves beyond the visible area of the control it wraps back to the left edge.
/// </remarks>
private void MoveLabelRight()
{
this.textLabel.Left += this._distanceInterval;
if (this.textLabel.Left >= this.Width)
{
// Wrap the text back to the left edge of the control.
this.textLabel.Left = -this.textLabel.Width;
}
}
#endregion // Methods
}
}