ARTICLE

Scrolling Text Marquee Control

Posted by Sam Hobbs Articles | Windows Forms C# January 13, 2010
The following article is a simple sample of a scrolling text marquee control for a Windows Forms application.
Reader Level:
 

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();
    }
}
 

Login to add your contents and source code to this article
post comment
     

The font, color and length of the text can be changed the same way as for any control.

Posted by Sam Hobbs Apr 18, 2013

I want to change font, color and length of the scrolling text. How could i do that..

Posted by Abdur Rahman Apr 18, 2013

For ASP use the ASP control for a marquee tag in HTML; you don't need anything from this article for ASP applications.

Posted by Sam Hobbs May 03, 2011

in Literal control i am using marquee when page opens its running marquee when i am selecting location (for change marquee text) its not working

Posted by neelesh masih May 03, 2011

How can i change the text color and also how can i set the time interval a little bit slow so that after completion of the given text it should come again

Posted by Dorababu M Dec 10, 2010
COMMENT USING
PREMIUM SPONSORS
Over-C is a holistic consortium of communications and technology specialists. We build, deploy and market both business as well as consumer products and solutions.
SPONSORED BY
  • PDF reports have never been easier to create. With our included WYSIWYG Designer, you can layout your reports, set up your data source and let DynamicPDF ReportWriter do the rest.
Get Career Advice from Experts