Results 1 to 1 of 1

Thread: Scrolling Text

  1. #1

    Thread Starter
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Scrolling Text

    VB version here.

    The attached project* contains a UserControl that, in turn, contains a Label. A Timer is used to move the Label and give the effect of scrolling text. It includes properties to change the speed and smoothness of the effect, as well as the direction and text. I've also added properties for the font and text colour.

    Note that I created this project in VS 2008. If you use an earlier version then just create your own project with a UserControl named MarqueeLabel. Add a Label named textLabel and a Timer named marqueeTimer, then paste in the following code:
    csharp Code:
    1. using System;
    2. using System.Collections.Generic;
    3. using System.ComponentModel;
    4. using System.Drawing;
    5. using System.Data;
    6. using System.Linq;
    7. using System.Text;
    8. using System.Windows.Forms;
    9.  
    10. namespace MarqueeLabel
    11. {
    12.     /// <summary>
    13.     /// Scolls text across the screen.
    14.     /// </summary>
    15.     public partial class MarqueeLabel : UserControl
    16.     {
    17. #region Types
    18.  
    19.         /// <summary>
    20.         /// Defines constants that indicate the direction the text scolls across the screen.
    21.         /// </summary>
    22.         public enum MarqueeDirection
    23.         {
    24.             /// <summary>
    25.             /// Text scrolls to the left, reappearing at the right edge of the control.
    26.             /// </summary>
    27.             Left,
    28.             /// <summary>
    29.             /// Text scrolls to the right, reappearing at the left edge of the control.
    30.             /// </summary>
    31.             Right
    32.         }
    33.  
    34. #endregion // Types
    35.  
    36. #region Variables
    37.  
    38.         /// <summary>
    39.         /// The direction the text scrolls across the screen.
    40.         /// </summary>
    41.         private MarqueeDirection _direction;
    42.         /// <summary>
    43.         /// The distance the text moves at each time interval.
    44.         /// </summary>
    45.         private int _distanceInterval = 5;
    46.  
    47. #endregion // Variables
    48.  
    49. #region Properties
    50.  
    51.         /// <summary>
    52.         /// The direction the text scrolls across the screen.
    53.         /// </summary>
    54.         /// <value>
    55.         /// <see cref="MarqueeDirection">Left</see> if the text scrolls to the left; <see cref="MarqueeDirection">Right</see> if the text scrolls to the right.
    56.         /// </value>
    57.         [Category("Marquee")]
    58.         [Description("The direction the text moves.")]
    59.         public MarqueeDirection Direction
    60.         {
    61.             get { return this._direction; }
    62.             set { this._direction = value; }
    63.         }
    64.  
    65.         /// <summary>
    66.         /// The distance the text moves each time interval.
    67.         /// </summary>
    68.         /// <value>
    69.         /// An <b>Int32</b> containing the number pixels the text will move.
    70.         /// </value>
    71.         /// <remarks>
    72.         /// The smaller the value the smoother the effect but the slower the text will move across the screen.
    73.         /// </remarks>
    74.         [Category("Marquee")]
    75.         [DefaultValue(5)]
    76.         [Description("The number of pixels the text moves each time.")]
    77.         public int DistanceInterval
    78.         {
    79.             get
    80.             {
    81.                 return this._distanceInterval;
    82.             }
    83.             set
    84.             {
    85.                 this._distanceInterval = value;
    86.             }
    87.         }
    88.  
    89.         /// <summary>
    90.         /// The font used to display the text.
    91.         /// </summary>
    92.         /// <value>
    93.         /// A <see cref="Font"/> object.
    94.         /// </value>
    95.         [Category("Marquee")]
    96.         [Description("The font face of the text.")]
    97.         public Font MarqueeFont
    98.         {
    99.             get
    100.             {
    101.                 return this.textLabel.Font;
    102.             }
    103.             set
    104.             {
    105.                 this.textLabel.Font = value;
    106.             }
    107.         }
    108.  
    109.         /// <summary>
    110.         /// Indicates whether the text will scroll or not.
    111.         /// </summary>
    112.         /// <value>
    113.         /// <b>True</b> if the text will scroll; otherwise, <b>False</b>.
    114.         /// </value>
    115.         [Category("Marquee")]
    116.         [DefaultValue(false)]
    117.         [Description("Whether or not the text moves.")]
    118.         public bool MarqueeEnabled
    119.         {
    120.             get
    121.             {
    122.                 return this.marqueeTimer.Enabled;
    123.             }
    124.             set
    125.             {
    126.                 this.marqueeTimer.Enabled = value;
    127.  
    128.                 if (!value)
    129.                 {
    130.                     this.textLabel.Left = 0;
    131.                 }
    132.             }
    133.         }
    134.  
    135.         /// <summary>
    136.         /// The text displayed on the label.
    137.         /// </summary>
    138.         /// <value>
    139.         /// A <b>String</b> containing the text to display.
    140.         /// </value>
    141.         [Category("Marquee")]
    142.         [Description("The text displayed on the control.")]
    143.         public string MarqueeText
    144.         {
    145.             get
    146.             {
    147.                 return this.textLabel.Text;
    148.             }
    149.             set
    150.             {
    151.                 this.textLabel.Text = value;
    152.             }
    153.         }
    154.  
    155.         /// <summary>
    156.         /// The colour used to display the text.
    157.         /// </summary>
    158.         /// <value>
    159.         /// A <see cref="Color"/> object.
    160.         /// </value>
    161.         [Category("Marquee")]
    162.         [Description("The colour of the text.")]
    163.         public Color TextColour
    164.         {
    165.             get
    166.             {
    167.                 return this.textLabel.ForeColor;
    168.             }
    169.             set
    170.             {
    171.                 this.textLabel.ForeColor = value;
    172.             }
    173.         }
    174.  
    175.         /// <summary>
    176.         /// The time interval between movements of the text.
    177.         /// </summary>
    178.         /// <value>
    179.         /// An <b>Int32</b> containing a number of milliseconds.
    180.         /// </value>
    181.         /// <remarks>
    182.         /// The smaller the value the faster the text will move.
    183.         /// </remarks>
    184.         [Category("Marquee")]
    185.         [DefaultValue(100)]
    186.         [Description("The number of milliseconds between text movements.")]
    187.         public int TimeInterval
    188.         {
    189.             get
    190.             {
    191.                 return this.marqueeTimer.Interval;
    192.             }
    193.             set
    194.             {
    195.                 this.marqueeTimer.Interval = value;
    196.             }
    197.         }
    198.  
    199. #endregion // Properties
    200.  
    201. #region Constructors
    202.  
    203.         public MarqueeLabel()
    204.         {
    205.             InitializeComponent();
    206.         }
    207.  
    208. #endregion // Constructors
    209.  
    210. #region Event Handlers
    211.  
    212.         private void marqueeTimer_Tick(object sender, EventArgs e)
    213.         {
    214.             switch (this._direction)
    215.             {
    216.                 case MarqueeDirection.Left:
    217.                     this.MoveLabelLeft();
    218.                     break;
    219.                 case MarqueeDirection.Right:
    220.                     this.MoveLabelRight();
    221.                     break;
    222.             }
    223.         }
    224.  
    225. #endregion // Event Handlers
    226.  
    227. #region Methods
    228.  
    229.     /// <summary>
    230.     /// Moves the text to the left by the current distance interval.
    231.     /// </summary>
    232.     /// <remarks>
    233.     /// If the text moves beyond the visible area of the control it wraps back to the right edge.
    234.     /// </remarks>
    235.     private void MoveLabelLeft()
    236.     {
    237.         this.textLabel.Left -= this._distanceInterval;
    238.  
    239.         if (this.textLabel.Right <= 0)
    240.         {
    241.             // Wrap the text back to the right edge of the control.
    242.             this.textLabel.Left = this.Width;
    243.         }
    244.     }
    245.  
    246.     /// <summary>
    247.     /// Moves the text to the right by the current distance interval.
    248.     /// </summary>
    249.     /// <remarks>
    250.     /// If the text moves beyond the visible area of the control it wraps back to the left edge.
    251.     /// </remarks>
    252.     private void MoveLabelRight()
    253.     {
    254.         this.textLabel.Left += this._distanceInterval;
    255.  
    256.         if (this.textLabel.Left >= this.Width)
    257.         {
    258.             // Wrap the text back to the left edge of the control.
    259.             this.textLabel.Left = -this.textLabel.Width;
    260.         }
    261.     }
    262.  
    263. #endregion // Methods
    264.     }
    265. }
    Note that you'll also have to connect the Timer's Tick event handler.

    * The solution attached to the VB version of this thread contains both the VB project and the C# project.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width