1

I am trying to make a rss news ticker which will display the text,the text need move from left to right

enter image description here

I made the the code and text is moving from left to right but after a specific time its not showing the full text,i will be adding more news from the admin pannel,each time i add the news the text is not showing after the first scroll

the below screenshot is after a specific amount of time,only a part of news is displaying enter image description here

Code used

int x = -800,y=1;
this.timer1.Tick += new System.EventHandler(this.timer1_Tick);

    private void timer1_Tick(object sender, System.EventArgs e)
        {

            label1.SetBounds(x, y, 1, 1);
            x++;
            if(x>=800)
            {
                x = 4;
            }

        }

Code for reading xml

private void StartRssThread()
        {
            List<RssChannel> channels = new List<RssChannel>();
            StringBuilder mergedFeed =  new StringBuilder();
            int mh = 0;


                int ms = 0;
                if (mh < 7)
                {
                    RssFeed DaFeed = RssFeed.Read("http://shjc.ae/rss/fileName.xml");
                    RssChannel DaChannel = (RssChannel)DaFeed.Channels[0];
                    channels.Add(DaChannel);
                    mergedFeed.AppendFormat(" {0}: ", DaChannel.Title);

                    foreach (RssItem sTrm in DaChannel.Items)
                    {
                        if (ms < 10)
                        {
                            mergedFeed.AppendFormat(" {0} |", sTrm.Title);
                            ms++;
                            mh++;
                        }
                    }
                }

            string dafeed = mergedFeed.ToString();
            mergedFeed = null;
            textBox1.Invoke(new UpdateUiCallback(this.UpdateUi), new string[] { dafeed });

        }
Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
riz
  • 69
  • 2
  • 8
  • 3
    The question is marked with "asp.net" and "winforms" tags, which does not make much sense. Are you working on a web application (asp.net) or desktop one (winforms)? – rs232 Nov 25 '19 at 10:29
  • [Windows Forms Marquee](https://stackoverflow.com/a/54272427/3110834) – Reza Aghaei Nov 25 '19 at 12:26

2 Answers2

4

Windows Forms Marquee Label - Horizontal

I've already posted an example of how to create a Marquee Label to animate text from top to bottom, by using a Timer and overriding OnPaint method of a custom draw control in this post: Windows Forms Top to Bottom Marquee Label.

In the following code, I've changed that example to animates the text horizontally from right to left or left to right. It's enough to set RightToLeft property of the control to change the direction. Also don't forget to set AutoSize to false.

enter image description here

Example - Right to Left and Left to Right Marquee Label in Windows Forms

using System;
using System.Drawing;
using System.Windows.Forms;
public class MarqueeLabel : Label
{
    Timer timer;
    public MarqueeLabel()
    {
        DoubleBuffered = true;
        timer = new Timer() { Interval = 100 };
        timer.Enabled = true;
        timer.Tick += Timer_Tick;
    }
    int? left;
    int textWidth = 0;
    private void Timer_Tick(object sender, EventArgs e)
    {
        if (RightToLeft == RightToLeft.Yes)
        {
            left += 3;
            if (left > Width)
                left = -textWidth;
        }
        else
        {
            left -= 3;
            if (left < -textWidth)
                left = Width;
        }
        Invalidate();
    }
    protected override void OnPaint(PaintEventArgs e)
    {
        e.Graphics.Clear(BackColor);
        var s = TextRenderer.MeasureText(Text, Font, new Size(0, 0),
            TextFormatFlags.TextBoxControl | TextFormatFlags.SingleLine);
        textWidth = s.Width;
        if (!left.HasValue) left = Width;
        var format = TextFormatFlags.TextBoxControl | TextFormatFlags.SingleLine |
            TextFormatFlags.VerticalCenter;
        if (RightToLeft == RightToLeft.Yes)
        {
            format |= TextFormatFlags.RightToLeft;
            if (!left.HasValue) left = -textWidth;
        }
        TextRenderer.DrawText(e.Graphics, Text, Font,
            new Rectangle(left.Value, 0, textWidth, Height),
            ForeColor, BackColor, format);
    }
    protected override void Dispose(bool disposing)
    {
        if (disposing)
            timer.Dispose();
        base.Dispose(disposing);
    }
}
Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
  • It's good enough to start. Later you can extend the control by adding a few properties to setup the movement step (instead of +=3 or -=3) or the movement interval (instead of 100 ms) or reset the animation on text change and some other properties. – Reza Aghaei Nov 25 '19 at 15:16
  • Just in case you wanted to extend the control, you can force `Label` to set `AutoSize = false` when you drop an instance of the control on form. To do so, take a look at [this post](https://stackoverflow.com/a/48353592/3110834). – Reza Aghaei Nov 25 '19 at 15:17
2

You are using hard coded values for the start and max value of x. From your question I think the text in the label has a dynamic length (correct?). If the text is of dynamic length the value of x should also be dynamic.

Also, x starts at -800. Then slowly grows to 800 and then it gets set to 4. This seems strange to me, if the first run started at -800, the second run may also need to start at -800.

Hope this somewhat helped you. If not, please provide more details (like why you chose -800, 800 and 4).

Martijn
  • 739
  • 9
  • 26
  • you are right,how can i get the length of the text which i am adding from backend – riz Nov 25 '19 at 10:42
  • @riz I think `TextRenderer.MeasureText( text, textBox1.Font );` should do it. Not sure but you can look up that function for more information – EpicKip Nov 25 '19 at 11:24
  • is it possible to add different images? how could i do it? –  May 03 '21 at 07:28