![The DougScrollingTextCtrl in an app.](/KB/edit/dougscrollingtextctrl/DougScrollingTextCtrl.gif)
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
{
public class DougScrollingTextCtrl : System.Windows.Forms.Control
{
private System.ComponentModel.Container components = null;
private Color m_Color1 = Color.Black;
private Color m_Color2 = Color.Gold;
private Font m_MyFont;
protected Timer m_Timer;
protected string sScrollText = null;
public DougScrollingTextCtrl()
{
m_Timer = new Timer();
m_Timer.Interval = 250;
m_Timer.Enabled = true;
m_Timer.Tick += new EventHandler( Animate );
}
public Color DougScrollingTextColor1
{
get { return m_Color1; }
set
{
m_Color1 = value;
Invalidate();
}
}
public Color DougScrollingTextColor2
{
get { return m_Color2; }
set
{
m_Color2 = value;
Invalidate();
}
}
void Animate( object sender, EventArgs e )
{
if( sScrollText == null )
{
sScrollText = Text + " ";
}
sScrollText = sScrollText.Substring( 1,
sScrollText.Length-1 ) + sScrollText.Substring( 0, 1 );
Invalidate();
}
void StartStop( object sender, EventArgs e )
{
m_Timer.Enabled = !m_Timer.Enabled;
}
protected override void OnTextChanged( EventArgs e )
{
sScrollText = null;
base.OnTextChanged( e );
}
protected override void OnClick( EventArgs e )
{
m_Timer.Enabled = !m_Timer.Enabled;
base.OnClick( e );
}
protected override void OnPaint( PaintEventArgs pe )
{
Brush MyBrush =
new System.Drawing.Drawing2D.LinearGradientBrush(
ClientRectangle, m_Color1, m_Color2, 10 );
m_MyFont = new Font( Font.Name, (Height*3)/4,
Font.Style, GraphicsUnit.Pixel );
pe.Graphics.DrawString( sScrollText, m_MyFont, MyBrush, 0, 0 );
base.OnPaint (pe);
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.
private DougScrollingText.DougScrollingTextCtrl m_DougScrollingTextCtrl;
this.m_DougScrollingTextCtrl =
new DougScrollingText.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!";
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