Click here to Skip to main content
Click here to Skip to main content

A C# Scrolling Text Control

, 27 Feb 2003
Rate this:
Please Sign up or sign in to vote.
This article shows you how to create a scrolling text/scrolling marquee control in C#.

The DougScrollingTextCtrl in an app.

Introduction

This article is intended to show you how easy it is to create a C# scrolling text control.

Background

After reading Alexandr Khilov's article Writing your Custom Control: step by step, I wanted to try and make my own C# control. I'm new to C# and Visual Studio.NET, so what better way to learn then to dive right in. This is the first control I've made, if I get more time I'll post the others.

The code

To create your own custom control, open Visual Studio and start a new project. Your project must be based on the C# Windows Control Library template. Call your project DougScrollingTextCtrl ( or whatever you want it to be called ) and then click OK.

Here is the complete DougScrollingTextCtrl code listing.

using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;

namespace DougScrollingText
{
    /// <summary>
    /// Summary description for DougScrollingTextCtrl.
    /// </summary>
    
    //////////////////////////////////////////////////////////////////
    //
    // Function: class DougScrollingTextCtrl.
    //
    // By: Doug 
    //
    // Date: 2/27/03
    //
    // Description: Create the control and derive 
    // it from System.Windows.Forms.Control.
    //
    ///////////////////////////////////////////////////////////////////
    //
    public class DougScrollingTextCtrl : System.Windows.Forms.Control
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.Container components = null; 
        private Color m_Color1 = Color.Black;  // First default color.
        private Color m_Color2 = Color.Gold;   // Second default color.
        private Font m_MyFont;   // For the font. 
        protected Timer m_Timer; // Timer for text animation.
        protected string sScrollText = null; // Text to be displayed 
                      // in the control.
        
        /// <summary>
        /// Add member variables.
        /// </summary> 


        ///////////////////////////////////////////////////////////////////
        //
        // Function: public DougScrollingTextCtrl()
        //
        // By: Doug 
        //
        // Date: 2/27/03
        //
        // Description: Constructor.
        //
        /////////////////////////////////////////////////////////////////// 
        //
        public DougScrollingTextCtrl()
        {
            m_Timer = new Timer(); 
            // Set the timer speed and properties.
            m_Timer.Interval = 250;
            m_Timer.Enabled = true;
            m_Timer.Tick += new EventHandler( Animate );
        } 
        // Add a color property.
        public Color DougScrollingTextColor1
        {
            get { return m_Color1; }
            set 
            {
                m_Color1 = value; 
                Invalidate();
            }
        } 
        // Add a color property.
        public Color DougScrollingTextColor2
        {
            get { return m_Color2; }
            set 
            {
                m_Color2 = value; 
                Invalidate();
            }
        } 
        ////////////////////////////////////////////////////////////////////
        //
        // Function: Animate( object sender, EventArgs e )
        //
        // By: Doug 
        //
        // Date: 2/27/03
        //
        // Description: Sets up the animation of the text.
        //
        /////////////////////////////////////////////////////////////////
        //
        void Animate( object sender, EventArgs e )
        {
            // sScrollText string is from the Text 
            // property, add 4 spaces after the string so 
            // everything is not bunche together.
            if( sScrollText == null )
            {
                sScrollText = Text + "    ";
            } 
            // Scroll text by triming one character at a time 
            // from the left, then adding that character to the 
           // right side of the control to make it look like scrolling text.
            sScrollText = sScrollText.Substring( 1, 
                sScrollText.Length-1 ) + sScrollText.Substring( 0, 1 );
            
            // Call Invalidate() to tell the windows form that
           // our control needs to be repainted.
            Invalidate();
        } 
        ///////////////////////////////////////////////////////////////////
        //
        // Function: StartStop( object sender, EventArgs e )
        //
        // By: Doug 
        //
        // Date: 2/27/03
        //
        // Description: Start and stop the timer.
        //
        /////////////////////////////////////////////////////////////////
        //
        void StartStop( object sender, EventArgs e )
        {
            m_Timer.Enabled = !m_Timer.Enabled;
        } 
        ////////////////////////////////////////////////////////////////////
        //
        // Function: protected override void OnTextChanged( EventArgs e )
        //
        // By: Doug 
        //
        // Date: 2/27/03
        //
        // Description: If/when the string text is changed, 
        // I need to update the sScrollText string.
        //
        ////////////////////////////////////////////////////////////////////
        //
        protected override void OnTextChanged( EventArgs e )
        {
            sScrollText = null; 
            base.OnTextChanged( e );
        } 
        ////////////////////////////////////////////////////////////////////
        //
        // Function: protected override void OnClick( EventArgs e )
        //
        // By: Doug 
        //
        // Date: 2/27/03
        //
        // Description: Handle the click event of the DougScrollingTextCtrl.
        //
        /////////////////////////////////////////////////////////////////////
        //
        protected override void OnClick( EventArgs e )
        {
            m_Timer.Enabled = !m_Timer.Enabled; 
            base.OnClick( e );
        } 
        //////////////////////////////////////////////////////////////////
        //
        // Function: protected override void OnPaint( PaintEventArgs pe )
        //
        // By: Doug 
        //
        // Date: 2/27/03
        //
        // Description: Paint the DougScrollingTextCtrl.
        //
        ////////////////////////////////////////////////////////////////
        //
        protected override void OnPaint( PaintEventArgs pe )
        {
            // This is a fancy brush that draws graded colors.
            Brush MyBrush = 
                new System.Drawing.Drawing2D.LinearGradientBrush( 
                  ClientRectangle, m_Color1, m_Color2, 10 ); 
            // Get the font and use it to draw text in the control.  
            // Resize to the height of the control if possible.
            m_MyFont = new Font( Font.Name, (Height*3)/4, 
                Font.Style, GraphicsUnit.Pixel ); 
            // Draw the text string in the control.
            pe.Graphics.DrawString( sScrollText, m_MyFont, MyBrush, 0, 0 ); 
            base.OnPaint (pe); 
            // Clean up variables..
            MyBrush.Dispose(); 
            m_MyFont.Dispose();
        }
    }
} 

To compile your new control, just press ctrl+shift+b. You'll see your new control is now ready to be added to an application.

Using the code

Implementing your new control/code should be pretty easy. First, while your target C# application is open, right click the toolbox, selecting Customize Toolbox, select the .NET Framework  Components tab, click Browse and locate the Control Library DLL ex: C:\\ ... DougScrollingTextCtrl\ ... \DougScrollingTextCtrl.dll. The component DougScrollingTextCtrl will now appear in the Toolbox. Now all you have to do is locate the DougScrollingTextCtrl at the bottom of your toolbox and drag it onto your application, or you can add the following code directly into your form1.cs class.

If you're hard coding the control manually ( not dragging it over from the tool box ) then use the code below. If you're dragging the control over from the tool box then ignore this code snippet.

// Add this line to the control decloration of your app.
private DougScrollingText.DougScrollingTextCtrl m_DougScrollingTextCtrl;

//Add these lines to your InitializeComponent() function call.
this.m_DougScrollingTextCtrl = 
  new DougScrollingText.DougScrollingTextCtrl(); 
//
// m_DougScrollingTextCtrl
//
this.m_DougScrollingTextCtrl.DougScrollingTextColor1 = 
  System.Drawing.Color.Crimson;
this.m_DougScrollingTextCtrl.DougScrollingTextColor2 = 
  System.Drawing.Color.Gold;
this.m_DougScrollingTextCtrl.Location = new System.Drawing.Point(24, 264);
this.m_DougScrollingTextCtrl.Name = "m_DougScrollingTextCtrl";
this.m_DougScrollingTextCtrl.Size = new System.Drawing.Size(240, 32);
this.m_DougScrollingTextCtrl.TabIndex = 7;
this.m_DougScrollingTextCtrl.Text = "Go Redskins!"; 
// Don't forget to add the control to your apps 
// this.Controls.AddRange(new System.Windows.Forms.Control[]{ 
// listing.
m_DougScrollingTextCtrl 

Points of Interest

When creating your own controls in C#, remember to add properties so that they are easy to use for yourself and others.

History

  • Initial release. DougScrollingTextCtrl version 1.0.0 2/28/03

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Manster
Web Developer
United States United States
Doug graduated college in 2000 with a degree in
Computer Information Systems. Since then Doug
has been working on software engineering projects
mostly for government consulting companies.
The majority of Doug's programming experience is in
windows development using C# and visual C++ with MFC.
Since October 2002, Doug has been using C# and has
been creating C# windows applications and ASP.NET web applications.

Comments and Discussions

 
GeneralThanks!!! Pinmemberpozdrav27-Nov-09 7:12 
GeneralError: The type 'DougScrollingTextCtrl.DougScrollingTextCtrl' already contains a definition for 'components' Pinmemberdblanchard17-Mar-09 17:23 
GeneralStrong Name Pinmemberjkjain7518-Aug-07 3:46 
Generalmaking the text scroll vertically Pinmemberjp_link26-Jul-07 19:16 
Generalscrolling text Pinmembereric@manas.kg11-Mar-07 3:00 
GeneralTransparency PinmemberRubenve14-Feb-07 11:06 
Generalhelp Pinmemberalpha_92-Aug-06 9:02 
QuestionInstall shield PinmemberBertus Kruger15-Mar-06 12:12 
GeneralSuggestion PinmemberChieu Luu12-Nov-05 6:21 
AnswerRe: Suggestion Pinmemberkeych16-Jul-07 6:15 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web04 | 2.8.141022.2 | Last Updated 28 Feb 2003
Article Copyright 2003 by Manster
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid