Results 1 to 7 of 7

Thread: Scrolling Text

  1. #1

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

    Scrolling Text

    C# 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. You could easily extend it to change the font, colour, etc.

    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:
    VB.NET Code:
    1. Imports System.ComponentModel
    2.  
    3. ''' <summary>
    4. ''' Scolls text across the screen.
    5. ''' </summary>
    6. Public Class MarqueeLabel
    7.  
    8. #Region " Types "
    9.  
    10.     ''' <summary>
    11.     ''' Defines constants that indicate the direction the text scolls across the screen.
    12.     ''' </summary>
    13.     Public Enum MarqueeDirection
    14.         ''' <summary>
    15.         ''' Text scrolls to the left, reappearing at the right edge of the control.
    16.         ''' </summary>
    17.         Left
    18.         ''' <summary>
    19.         ''' Text scrolls to the right, reappearing at the left edge of the control.
    20.         ''' </summary>
    21.         Right
    22.     End Enum
    23.  
    24. #End Region 'Types
    25.  
    26. #Region " Variables "
    27.  
    28.     ''' <summary>
    29.     ''' The direction the text scrolls across the screen.
    30.     ''' </summary>
    31.     Private _direction As MarqueeDirection
    32.     ''' <summary>
    33.     ''' The distance the text moves at each time interval.
    34.     ''' </summary>
    35.     Private _distanceInterval As Integer = 5
    36.  
    37. #End Region 'Variables
    38.  
    39. #Region " Properties "
    40.  
    41.     ''' <summary>
    42.     ''' The direction the text scrolls across the screen.
    43.     ''' </summary>
    44.     ''' <value>
    45.     ''' <see cref="MarqueeDirection">Left</see> if the text scrolls to the left; <see cref="MarqueeDirection">Right</see> if the text scrolls to the right.
    46.     ''' </value>
    47.     <Category("Marquee")> _
    48.     <Description("The direction the text moves.")> _
    49.     Public Property Direction() As MarqueeDirection
    50.         Get
    51.             Return Me._direction
    52.         End Get
    53.         Set(ByVal value As MarqueeDirection)
    54.             Me._direction = value
    55.         End Set
    56.     End Property
    57.  
    58.     ''' <summary>
    59.     ''' The distance the text moves each time interval.
    60.     ''' </summary>
    61.     ''' <value>
    62.     ''' An <b>Int32</b> containing the number pixels the text will move.
    63.     ''' </value>
    64.     ''' <remarks>
    65.     ''' The smaller the value the smoother the effect but the slower the text will move across the screen.
    66.     ''' </remarks>
    67.     <Category("Marquee")> _
    68.     <DefaultValue(5)> _
    69.     <Description("The number of pixels the text moves each time.")> _
    70.     Public Property DistanceInterval() As Integer
    71.         Get
    72.             Return Me._distanceInterval
    73.         End Get
    74.         Set(ByVal value As Integer)
    75.             Me._distanceInterval = value
    76.         End Set
    77.     End Property
    78.  
    79.     ''' <summary>
    80.     ''' Indicates whether the text will scroll or not.
    81.     ''' </summary>
    82.     ''' <value>
    83.     ''' <b>True</b> if the text will scroll; otherwise, <b>False</b>.
    84.     ''' </value>
    85.     <Category("Marquee")> _
    86.     <DefaultValue(False)> _
    87.     <Description("Whether or not the text moves.")> _
    88.     Public Property MarqueeEnabled() As Boolean
    89.         Get
    90.             Return Me.marqueeTimer.Enabled
    91.         End Get
    92.         Set(ByVal value As Boolean)
    93.             Me.marqueeTimer.Enabled = value
    94.  
    95.             If Not value Then
    96.                 Me.textLabel.Left = 0
    97.             End If
    98.         End Set
    99.     End Property
    100.  
    101.     ''' <summary>
    102.     ''' The text displayed on the label.
    103.     ''' </summary>
    104.     ''' <value>
    105.     ''' A <b>String</b> containing the text to display.
    106.     ''' </value>
    107.     <Category("Marquee")> _
    108.     <Description("The text displayed on the control.")> _
    109.     Public Property MarqueeText() As String
    110.         Get
    111.             Return Me.textLabel.Text
    112.         End Get
    113.         Set(ByVal value As String)
    114.             Me.textLabel.Text = value
    115.         End Set
    116.     End Property
    117.  
    118.     ''' <summary>
    119.     ''' The time interval between movements of the text.
    120.     ''' </summary>
    121.     ''' <value>
    122.     ''' An <b>Int32</b> containing a number of milliseconds.
    123.     ''' </value>
    124.     ''' <remarks>
    125.     ''' The smaller the value the faster the text will move.
    126.     ''' </remarks>
    127.     <Category("Marquee")> _
    128.     <DefaultValue(100)> _
    129.     <Description("The number of milliseconds between text movements.")> _
    130.     Public Property TimeInterval() As Integer
    131.         Get
    132.             Return Me.marqueeTimer.Interval
    133.         End Get
    134.         Set(ByVal value As Integer)
    135.             Me.marqueeTimer.Interval = value
    136.         End Set
    137.     End Property
    138.  
    139. #End Region 'Properties
    140.  
    141. #Region " Event Handlers "
    142.  
    143.     Private Sub marqueeTimer_Tick(ByVal sender As Object, _
    144.                                   ByVal e As EventArgs) Handles marqueeTimer.Tick
    145.         Select Case Me._direction
    146.             Case MarqueeDirection.Left
    147.                 Me.MoveLabelLeft()
    148.             Case MarqueeDirection.Right
    149.                 Me.MoveLabelRight()
    150.         End Select
    151.     End Sub
    152.  
    153. #End Region 'Event Handlers
    154.  
    155. #Region " Methods "
    156.  
    157.     ''' <summary>
    158.     ''' Moves the text to the left by the current distance interval.
    159.     ''' </summary>
    160.     ''' <remarks>
    161.     ''' If the text moves beyond the visible area of the control it wraps back to the right edge.
    162.     ''' </remarks>
    163.     Private Sub MoveLabelLeft()
    164.         Me.textLabel.Left -= Me._distanceInterval
    165.  
    166.         If Me.textLabel.Right <= 0 Then
    167.             'Wrap the text back to the right edge of the control.
    168.             Me.textLabel.Left = Me.Width
    169.         End If
    170.     End Sub
    171.  
    172.     ''' <summary>
    173.     ''' Moves the text to the right by the current distance interval.
    174.     ''' </summary>
    175.     ''' <remarks>
    176.     ''' If the text moves beyond the visible area of the control it wraps back to the left edge.
    177.     ''' </remarks>
    178.     Private Sub MoveLabelRight()
    179.         Me.textLabel.Left += Me._distanceInterval
    180.  
    181.         If Me.textLabel.Left >= Me.Width Then
    182.             'Wrap the text back to the left edge of the control.
    183.             Me.textLabel.Left = -Me.textLabel.Width
    184.         End If
    185.     End Sub
    186.  
    187. #End Region 'Methods
    188.  
    189. End Class
    EDIT: The solution attached now contains a VB project and a C# project. I've also added properties for font and text colour.
    Attached Files Attached Files
    Last edited by jmcilhinney; Oct 25th, 2017 at 07:37 PM.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  2. #2

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

    Re: Scrolling Text

    The solution attached to the first post now contains a VB project and a C# project. I've also added properties for font and text colour.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    Addicted Member
    Join Date
    Oct 2009
    Location
    Clive, IA in America!!!!
    Posts
    204

    Re: Scrolling Text

    Is there any way to wrap the text from one side of the form to the other?

    This way, as soon as part of the label disappears on one side of the form, it appears on the other side...

    VBNetDude - Thinking Programmatically
    By Silver Seal Software

    Don't forget to mark your thread as "Resolved" using the Thread Tools menu on top. And don't forget to rate the answers that help you the most!

  4. #4

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

    Re: Scrolling Text

    Quote Originally Posted by VBNetDude View Post
    Is there any way to wrap the text from one side of the form to the other?

    This way, as soon as part of the label disappears on one side of the form, it appears on the other side...
    Not as it stands. You could add a second Label with the same Text and have them side by side. As one Label went completely off one end you could move it back to the other end. Alternatively you could use GDI+ and draw the text twice.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5
    Addicted Member
    Join Date
    Oct 2009
    Location
    Clive, IA in America!!!!
    Posts
    204

    Re: Scrolling Text

    Here is a version that wraps over to the other end. Since this is in JMC's CodeBank Post, this counts as JMC's work!
    Attached Files Attached Files
    Last edited by VBNetDude; Nov 19th, 2009 at 05:44 PM.

    VBNetDude - Thinking Programmatically
    By Silver Seal Software

    Don't forget to mark your thread as "Resolved" using the Thread Tools menu on top. And don't forget to rate the answers that help you the most!

  6. #6

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

    Re: Scrolling Text

    Quote Originally Posted by VBNetDude View Post
    Here is a version that wraps over to the other end. Since this is in JMC's CodeBank Post, this counts as JMC's work!
    All your c0d is belong to us!
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7
    Addicted Member
    Join Date
    Oct 2009
    Location
    Clive, IA in America!!!!
    Posts
    204

    Re: Scrolling Text


    VBNetDude - Thinking Programmatically
    By Silver Seal Software

    Don't forget to mark your thread as "Resolved" using the Thread Tools menu on top. And don't forget to rate the answers that help you the most!

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