Scrolling Text Marquee Control


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 ScrollingTextControl.cs file in the attached 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.

The following is the UserControl.
public partial class ScrollingTextControl : UserControl
{
    Timer MarqueeTimer = new Timer();
    String CurrentText = "Scrolling text";
    String OurText;

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

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

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

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

    void MarqueeUpdate(object sender, EventArgs e)
    {
        CurrentText = CurrentText.Substring(1) + CurrentText[0];
        Invalidate();
    }
}


Similar Articles