-
Apr 14th, 2010, 12:51 PM
#1
Thread Starter
Lively Member
[SOLVED] Marquee Label
Hey guys,
I'm looking on how to make something like a NewsTicker.
Imagine that I have the text "The sky is blue and today will not rain."
And I want it to marquee something like:
Well, let me explain, I want to always be showing something on the label, and when it ends, before ending it starts the beggining again..
Any help would be appreciated
Thanks
Last edited by SirPereira; May 3rd, 2010 at 12:11 PM.
-
Apr 14th, 2010, 12:53 PM
#2
Lively Member
-
Apr 14th, 2010, 12:56 PM
#3
Re: Marquee Label
Utilizing a timer, play around with the .Left property of the component. Increasing it's value will move it right and decreasing it's value will move it left.
ie.
You'll then need to do something with the bounds of the form. I couldn't tell you what exactly, but some Google searches should yield some results.
*On a side note... marquee text is a bit unprofessional in my opinion. Thankfully, you don't see them in websites much anymore and I don't think I've ever seen them in desktop applications.
CodeBank contributions: Process Manager, Temp File Cleaner
Originally Posted by SJWhiteley
"game trainer" is the same as calling the act of robbing a bank "wealth redistribution"....
-
Apr 14th, 2010, 12:59 PM
#4
Thread Starter
Lively Member
Re: Marquee Label
Originally Posted by weirddemon
Utilizing a timer, play around with the .Left property of the component. Increasing it's value will move it right and decreasing it's value will move it left.
ie.
You'll then need to do something with the bounds of the form. I couldn't tell you what exactly, but some Google searches should yield some results.
*On a side note... marquee text is a bit unprofessional in my opinion. Thankfully, you don't see them in websites much anymore and I don't think I've ever seen them in desktop applications.
Well, but this is not a real marquee. It is for a newsticker.
But what you gave me to me, I already know :P As I said, I need it to be showing the beggining when it didn't achieved the end.
-
Apr 14th, 2010, 01:11 PM
#5
Re: Marquee Label
But what you gave me to me, I already know :P As I said, I need it to be showing the beggining when it didn't achieved the end.
It's important to be very clear when asking your question. You can't assume we know what you know. When I read your question, it sounded like you needed to know how to accomplish the entire task. Had you said you knew how to scroll, I wouldn't have mentioned that.
Now, I did this and found this.
I tested out the example posted by John Anthony Oliver, and it seems to work just fine. You'll need to edit it for your needs, but it seems pretty solid.
CodeBank contributions: Process Manager, Temp File Cleaner
Originally Posted by SJWhiteley
"game trainer" is the same as calling the act of robbing a bank "wealth redistribution"....
-
Apr 14th, 2010, 01:15 PM
#6
Addicted Member
Re: Marquee Label
Label1.Left += 1
If(Label1.Left >= Me.Width)Then
Label1.left = 0 - Label1.width
end if
Throw that into a timer, and it should work.
[edit]
Oh wait, I read the Question wrong, nevermind.
Last edited by Mal1t1a; Apr 14th, 2010 at 01:21 PM.
-
Apr 14th, 2010, 01:17 PM
#7
Thread Starter
Lively Member
Re: Marquee Label
Originally Posted by SirPereira
Well, let me explain, I want to always be showing something on the label, and when it ends, before ending it starts the beggining again..
Sorry for correcting you, but I really said it :P
I will try, than I will say something
Thanks
-
Apr 14th, 2010, 01:21 PM
#8
Re: Marquee Label
That doesn't say you know how to do the scrolling. All it says is that you want to loop the text.
Nowhere in your original post did you tell us you knew how to do the scrolling. All I'm saying is that you need to tell us, not assume, what you know or have already tried.
Either way, the example I posted to should work and so should the other guys.
CodeBank contributions: Process Manager, Temp File Cleaner
Originally Posted by SJWhiteley
"game trainer" is the same as calling the act of robbing a bank "wealth redistribution"....
-
Apr 14th, 2010, 01:24 PM
#9
Thread Starter
Lively Member
Re: Marquee Label
I will see.
Sorry for my misunderstanding then
-
Apr 14th, 2010, 04:31 PM
#10
Thread Starter
Lively Member
Re: Marquee Label
Well, until now I couldn't get it to work.
Tried the codes, but didn't understood this one. What this actually does?
http://social.msdn.microsoft.com/for...-e74187ad17fd/
Thanks.
-
Apr 14th, 2010, 06:23 PM
#11
Re: Marquee Label
If I understand your problem correctly, you want wrap the part of the label that has gone outside the boundaries back to the start so that you can see the entire string, instead of having to wait for it to go outside the boundary completely.
So, for example, if you have a string "abcdefg", scrolling to the left, then you get this
instead of
To do that, you'll need to draw the string twice. Once for the 'main' string ("defg" in my example), and once for the "wrapped" string ("abc" in my example). You don't have to worry about which part of the string to draw; you just draw the entire string twice and whatever falls outside the boundaries is invisible anyway.
You'll need to separate the two strings by some amount, and this is the tricky part that I haven't yet figured out myself (I needed this a short while ago too).
If the length of the string is less than the width of your label, then you can separate the two strings by the width of the label. For example, if you're using DrawString to draw the string, you would have something like
Code:
e.Graphics.DrawString(text, Label1.Font, xPosition, 0)
e.Graphics.DrawString(text, Label1.Font, xPosition + Label1.Width, 0)
(text is the string and xPosition is the x-coordinate of the text, which is changed on a timer tick).
Once the xPosition is less than minus the width of the string (use Graphics.MeasureString to determine this), you need to put it back at the end (xPosition = Label1.Width).
But, if the string is longer than the label itself, you'll get into trouble. The two strings will start to overlap. You can just use a higher separation (instead of the label width), but then you'll get into trouble with the 'wrap around condition' (setting xPosition back to label width); you'll see a jump in the position...
I haven't solved this yet, but I haven't really tried either so if I solve it I'll let you know.
EDIT
This is what I was using. Note that this is in an inherited Label, so "Me" refers to the label itself:
vb.net Code:
Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs) * * MyBase.OnPaint(e) * * * * Dim size As SizeF = e.Graphics.MeasureString(DisplayText, Font) * * Dim w As Integer = CInt(size.Width) * * Dim h As Integer = CInt(size.Height) * * 'If the string has moved off the label, put it back at the end * * If position < -w Then * * * * position = Me.Width - w * * End If * * 'The separation between the 'real' string and the mirror string is Me.Width * * Dim mirrorPosition As Integer = position + Me.Width * * 'Drawing the two strings (note, (Me.Height - h)/2 ensures that the string is drawn in the center, vertically, instead of at the top) * * e.Graphics.DrawString(DisplayText, Font, New SolidBrush(ForeColor), position, (Me.Height - h) / 2) * * e.Graphics.DrawString(DisplayText, Font, New SolidBrush(ForeColor), mirrorPosition, (Me.Height - h) / 2) End Sub
This still has the problem I mentioned though, it will get overlapping text if the string is too long.
Last edited by NickThissen; Apr 14th, 2010 at 06:28 PM.
-
Apr 19th, 2010, 09:20 AM
#12
Thread Starter
Lively Member
Re: Marquee Label
Sorry, but how can I add that OnPaint event to the label?
And, any news about solving it?
Regards
-
Apr 19th, 2010, 09:50 AM
#13
Re: Marquee Label
You get the OnPaint method override if you inherit the Label.
I did solve my issue, at least half. I think it only works for scrolling to the left though, if you need it to scroll to the right you'd want to add another check in the OnPaint statement to wrap the label back to the left, once it reaches the right side.
Anyway, my code was in C#, so I ran it through a translator quickly, so there might be some slight syntax errors:
Code:
Public Class ScrollLabel
    Inherits Label
    #Region "Private Fields"
   
    Private _Timer As New Timer()
    Private _MirrorPosition As Single = 0F
    Private _Position As Single = 0F
   
    #End Region
   
    #Region "Properties"
   
    ''' <summary>
    ''' The scroll speed in pixels / second. Use negative values for scrolling to the left.
    ''' </summary>
Private _ScrollSpeed As Single
    Public Property ScrollSpeed() As Single
        Get
            Return _ScrollSpeed
        End Get
        Set(ByVal value As Single)
            _ScrollSpeed = value
        End Set
    End Property
   
    <Browsable(False)> _
    Public Overloads Overrides Property Text() As String
        Get
            Return MyBase.Text
        End Get
        Set(ByVal value As String)
            MyBase.Text = value
        End Set
    End Property
   
    ''' <summary>
    ''' The scrolling text.
    ''' </summary>
Private _DisplayText As String
    Public Property DisplayText() As String
        Get
            Return _DisplayText
        End Get
        Set(ByVal value As String)
            _DisplayText = value
        End Set
    End Property
   
    ''' <summary>
    ''' The separation between the real text and the mirror text.
    ''' </summary>
Private _MirrorTextSeparation As Integer
    Public Property MirrorTextSeparation() As Integer
        Get
            Return _MirrorTextSeparation
        End Get
        Set(ByVal value As Integer)
            _MirrorTextSeparation = value
        End Set
    End Property
   
    Public Enum VerticalAlignment
        Top
        Center
        Bottom
    End Enum
    Private _TextAlignment As VerticalAlignment
    Public Property TextAlignment() As VerticalAlignment
        Get
            Return _TextAlignment
        End Get
        Set(ByVal value As VerticalAlignment)
            _TextAlignment = value
            Me.Invalidate()
        End Set
    End Property
   
    <Browsable(False)> _
    Public Overloads Overrides Property TextAlign() As System.Drawing.ContentAlignment
        Get
            Return MyBase.TextAlign
        End Get
        Set(ByVal value As System.Drawing.ContentAlignment)
            MyBase.TextAlign = value
        End Set
    End Property
   
    #End Region
   
    Public Sub New()
        Me.DisplayText = Me.Text
        Me.Text = [String].Empty
       
        _Timer.Interval = 50
        _Timer.Enabled = True
        AddHandler _Timer.Tick, AddressOf _Timer_Tick
       
        Me.AutoSize = False
        Me.TextAlignment = VerticalAlignment.Center
        Me.ScrollSpeed = -1F
        Me.MirrorTextSeparation = 0
    End Sub
   
    Private Sub _Timer_Tick(ByVal sender As Object, ByVal e As EventArgs)
        If Not Me.DesignMode Then
            _Position += Me.ScrollSpeed
            _MirrorPosition += Me.ScrollSpeed
           
            Me.Invalidate()
        End If
    End Sub
   
    Protected Overloads Overrides Sub OnPaint(ByVal e As PaintEventArgs)
        MyBase.OnPaint(e)
       
        Dim size As SizeF = e.Graphics.MeasureString(Me.DisplayText, Me.Font)
       
        Dim y As Single = 0F
        Select Case Me.TextAlignment
            Case VerticalAlignment.Top
                y = 0F           
            Case VerticalAlignment.Bottom
                y = Me.Height - size.Height          
            Case VerticalAlignment.Center
                y = (Me.Height - size.Height) / 2F
        End Select
       
        Dim sep As Single = Math.Max(Me.Width, size.Width) + Me.MirrorTextSeparation
        Dim mirrorPosition As Single = _Position + sep
       
        If _Position < Me.Width - sep - size.Width Then
            _Position += sep
        End If
       
        e.Graphics.DrawString(Me.DisplayText, Me.Font, New SolidBrush(Me.ForeColor), _Position, y)
        e.Graphics.DrawString(Me.DisplayText, Me.Font, New SolidBrush(Me.ForeColor), mirrorPosition, y)
    End Sub
   
End Class
To use: simply add a new class to your project, call it ScrollLabel, paste this code into it and then Build your solution. It should now appear in your toolbox. Note that you need to use its DisplayText property to set the scrolling text, not its Text property.
-
Apr 19th, 2010, 10:55 AM
#14
Re: Marquee Label
Hey,
I seem to remember that this CodeBank submission might do exactly what you are after:
http://www.vbforums.com/showthread.php?t=540356
Gary
-
Apr 26th, 2010, 03:47 PM
#15
Thread Starter
Lively Member
Re: Marquee Label
Yeah, it is something like that, but smoother and words should not appear as the space appear, it needs to be already there when it shows up, and not showing suddenly.
-
Apr 27th, 2010, 01:10 AM
#16
Re: Marquee Label
Hey,
I am sure you could use that post as a starting point, and build on it from there.
Gary
-
Apr 27th, 2010, 02:22 AM
#17
Re: Marquee Label
Did you try mine? I think it does what you want, although I'm not actually quite sure what you want
-
Apr 27th, 2010, 02:48 AM
#18
Thread Starter
Lively Member
Re: Marquee Label
NickThissen, sorry, I jumped over ur post by distraction..
I'm trying to set it up, but, I already have the control in my App, in the load of Form, I already set the displayText, but nothing is showing up.
Already tried putting the code in the ScrollLabel1 Paint event and Form Paint event.
Nothing
-
Apr 27th, 2010, 03:31 AM
#19
Re: Marquee Label
You just need to add the code in my post to a new class and then build your solution. The ScrollLabel control will then show up in your toolbox, and you just drag it to your form and use it like any other label (except that you use the DisplayText property instead of the Text property).
-
Apr 27th, 2010, 09:03 AM
#20
Thread Starter
Lively Member
Re: Marquee Label
Even with just adding, and modifying the DisplayText, in the design, it won't show anything, and in run-time nothing shows up too..
This is my current code:
vb.net Code:
Imports System.ComponentModel Public Class ScrollLabel Inherits Label #Region "Private Fields" Private _Timer As New Timer() Private _MirrorPosition As Single = 0.0F Private _Position As Single = 0.0F #End Region #Region "Properties" ''' <summary> ''' The scroll speed in pixels / second. Use negative values for scrolling to the left. ''' </summary> Private _ScrollSpeed As Single Public Property ScrollSpeed() As Single Get Return _ScrollSpeed End Get Set(ByVal value As Single) _ScrollSpeed = value End Set End Property <Browsable(False)> _ Public Overloads Overrides Property Text() As String Get Return MyBase.Text End Get Set(ByVal value As String) MyBase.Text = value End Set End Property ''' <summary> ''' The scrolling text. ''' </summary> Private _DisplayText As String Public Property DisplayText() As String Get Return _DisplayText End Get Set(ByVal value As String) _DisplayText = value End Set End Property ''' <summary> ''' The separation between the real text and the mirror text. ''' </summary> Private _MirrorTextSeparation As Integer Public Property MirrorTextSeparation() As Integer Get Return _MirrorTextSeparation End Get Set(ByVal value As Integer) _MirrorTextSeparation = value End Set End Property Public Enum VerticalAlignment Top Center Bottom End Enum Private _TextAlignment As VerticalAlignment Public Property TextAlignment() As VerticalAlignment Get Return _TextAlignment End Get Set(ByVal value As VerticalAlignment) _TextAlignment = value Me.Invalidate() End Set End Property <Browsable(False)> _ Public Overloads Overrides Property TextAlign() As System.Drawing.ContentAlignment Get Return MyBase.TextAlign End Get Set(ByVal value As System.Drawing.ContentAlignment) MyBase.TextAlign = value End Set End Property #End Region Public Sub New() Me.DisplayText = Me.Text Me.Text = [String].Empty _Timer.Interval = 50 _Timer.Enabled = True AddHandler _Timer.Tick, AddressOf _Timer_Tick Me.AutoSize = False Me.TextAlignment = VerticalAlignment.Center Me.ScrollSpeed = -1.0F Me.MirrorTextSeparation = 0 End Sub Private Sub _Timer_Tick(ByVal sender As Object, ByVal e As EventArgs) If Not Me.DesignMode Then _Position += Me.ScrollSpeed _MirrorPosition += Me.ScrollSpeed Me.Invalidate() End If End Sub Protected Overloads Overrides Sub OnPaint(ByVal e As PaintEventArgs) MyBase.OnPaint(e) Dim size As SizeF = e.Graphics.MeasureString(Me.DisplayText, Me.Font) Dim y As Single = 0.0F Select Case Me.TextAlignment Case VerticalAlignment.Top y = 0.0F Case VerticalAlignment.Bottom y = Me.Height - size.Height Case VerticalAlignment.Center y = (Me.Height - size.Height) / 2.0F End Select Dim sep As Single = Math.Max(Me.Width, size.Width) + Me.MirrorTextSeparation Dim mirrorPosition As Single = _Position + sep If _Position < Me.Width - sep - size.Width Then _Position += sep End If e.Graphics.DrawString(Me.DisplayText, Me.Font, New SolidBrush(Me.ForeColor), _Position, y) e.Graphics.DrawString(Me.DisplayText, Me.Font, New SolidBrush(Me.ForeColor), mirrorPosition, y) End Sub End Class
-
Apr 30th, 2010, 12:24 PM
#21
Thread Starter
Lively Member
Re: Marquee Label
Anyone ?
-
Apr 30th, 2010, 12:44 PM
#22
Re: Marquee Label
I've no idea what the problem is, as it works just fine for me. Did you modify any properties other than the DisplayText?
-
Apr 30th, 2010, 01:51 PM
#23
Re: Marquee Label
Hey,
Can you provide some details as to exactly why it isn't working for you. I think this is one of those situations where the requirements haven't been correctly explained/uinderstood.
Perhaps some screenshots will help.
Gary
-
May 3rd, 2010, 10:22 AM
#24
Thread Starter
Lively Member
Re: Marquee Label
Well, it just don't show nothing at all..
Just this:
Btw, do you know how to do something but with a PictureBox control?
Because Imagine I want to add icons before the label. How could I do that and make everything scroll?
Thanks.
Last edited by SirPereira; May 3rd, 2010 at 11:10 AM.
-
May 3rd, 2010, 11:31 AM
#25
Re: Marquee Label
Ah, AutoSize, set it to false
-
May 3rd, 2010, 11:48 AM
#26
Thread Starter
Lively Member
Re: Marquee Label
Thank you . Finally got it working.
And what about moving labels with images, whatever?
-
May 3rd, 2010, 11:51 AM
#27
Re: Marquee Label
Yes, the problem is the AutoSize property. I forgot about that. I am setting it in the constructor of the control but apparently this setting gets overridden or something, so that it's still False when you drag the control to the form.
If you want to add icons, that wouldn't be too much of a problem. You don't need a picturebox for that, the same control will do just fine. The fact that it's a label isn't important at all. I was originally letting it inherit Control directly, which would work just the same, but a Control does not support transparent background colors while a Label does, so I thought a label was more appropriate. This is also the reason I'm not using the Text property; the scrolling text is painted over the actual Text of the label. I'm sure you could get around that and be able to use the actual Text property but I never intended to make this into a real control, just something I used once, so I was fine with it the way it was.
Anyway, my point was that you can just use the same control, you'd just have to modify the OnPaint method (and add a property that accepts an Image; in fact I think a label already has an Image, no?). Besides using DrawString to draw the text, you would use DrawImage to draw the image in the text position minus the width of the image (plus some offset probably). That's all. You'd probably want to take into account the width of the image too when determining the 'mirror position' of the mirror text, otherwise the mirror text could overlap the image if it's large enough. The logic is all in the code already, you just have to add the width of the image.
-
May 3rd, 2010, 12:11 PM
#28
Thread Starter
Lively Member
Re: Marquee Label
So much thank you!
Now I've got some ideas to make the label a bit better
Thank you a lot, really.
-
Jun 18th, 2010, 11:42 AM
#29
Thread Starter
Lively Member
Re: [SOLVED] Marquee Label
Hello again, sorry for reviving the topic, but I'm not seeing how to implement the image field in the Label... any suggestions?
-
Jun 29th, 2010, 11:45 AM
#30
Thread Starter
Lively Member
Re: [SOLVED] Marquee Label
-
Jun 29th, 2010, 12:11 PM
#31
Re: [SOLVED] Marquee Label
I'm not sure what else to tell you besides what I already did. Did you try implementing it and got into some issues? If so, what issues?
I am painting the text in the OnPaint method. Besides painting the text, you can also paint the image using e.Graphics.DrawImage.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|