Scrolling Text Marquee Control


Click here to change the theme.

ScrollingTextControlSample.zip

The HTML MARQUEE Element creates a scrolling text marquee, but there is not an equivalent provided by Microsoft for desktop applications. The following is a simple sample of a scrolling text marquee control for a Windows Forms application.

The control uses a Timer object. For each timer tick, the text to scroll is updated by moving the current first character to the last character position. This causes the affect of the text continually scrolling. The text is written directly to the control’s window using TextRenderer.DrawText.

To use the control, simply add the UserControl to a form and then during execution, such as in the form’s constructor, set the UserControl’s Text property to the text to scroll. If you are unfamiliar with the use of UserControls, then simply add the following ScrollingTextControl.cs file to the project, then build the project to ensure that Visual Studio sees it as a UserControl, then drag the ScrollingTextControl from the Toolbox onto the form.

using System;
using System.Drawing;
using System.Windows.Forms;

namespace ScrollingTextControlSample
{
public partial class ScrollingTextControl : UserControl
{
Timer MarqueeTimer = new Timer();
String CurrentText = "Scrolling text";
String OurText;
UInt32 OurScrollAmount = 1;
BehaviorType OurBehavior = BehaviorType.Scroll;
DirectionType OurDirection = DirectionType.Forward;

enum BehaviorType {Scroll, Alternate, Slide}

enum DirectionType {Forward, Backward}

public ScrollingTextControl()
{
InitializeComponent();
MarqueeTimer.Interval = 85;
MarqueeTimer.Enabled = true;
MarqueeTimer.Tick += new EventHandler(MarqueeUpdate);
}

public int ScrollDelay
{
get { return MarqueeTimer.Interval; }
set { MarqueeTimer.Interval = value; }
}

public UInt32 ScrollAmount
{
get { return OurScrollAmount; }
set { OurScrollAmount = value; }
}

public DirectionType Direction
{
get { return OurDirection; }
set { OurDirection = value; }
}

public BehaviorType Behavior
{
get { return OurBehavior; }
set { OurBehavior = value; }
}

public override String Text
{
get { return OurText; }
set { OurText = value; CurrentText = OurText; }
}

private void ScrollingTextControl_Paint(object sender, PaintEventArgs e)
{
// ToDo: Padding
TextRenderer.DrawText(e.Graphics, CurrentText, Font, ClientRectangle,
SystemColors.ControlText);
}

void MarqueeUpdate(object sender, EventArgs e)
{
// ToDo: Direction
// ToDo: Loop (number of times to play)
CurrentText = CurrentText.Substring((int)OurScrollAmount) + CurrentText[0];
Invalidate();
}
}
}