Quantcast
Channel: Windows Forms General forum
Viewing all articles
Browse latest Browse all 12583

How can I add the string text to the method that get string[] to display the text in my UserControl ?

$
0
0

This is the code in Form1 i tried now:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net;
using System.Text.RegularExpressions;

namespace RssNews
{
    public partial class Form1 : Form
    {
        string readableRss;
        string t = "";

        public Form1()
        {
            InitializeComponent();
        }

        
            

        private void Form1_Load(object sender, EventArgs e)
        {
            
            readableRss = RssReader.covertRss("http://rotter.net/rss/rotternews.xml");
            string[] lines = Regex.Split(readableRss, "\r\n");
            this.newsFeed1.NewsTextFeed = new string[lines.Length];
            for (int i = 0; i < lines.Length; i++)
            {
                t = Convert.ToString(lines[i]);
                SetupText(new string[] { t });
            }
            this.newsFeed1.TextColor = new Color[0];
            SetupColors(new Color[] { Color.Blue });
            //this.newsFeed1.NewsTextFeed = new string[5];
            //this.newsFeed1.TextColor = new Color[5];
            //SetupText(new string[] { "Hello everyone this is the weather for today", "Text2", "hello world this is a test for long text what do you think", "Text 4 -> 4", "Text Nr. 5" });

            //Color.Blue, Color.Lime, Color.Maroon, Color.FromArgb(255, 71, 71, 255), Color.BurlyWood });
            //set this Variable to the Height of the Control to display one label at a time
            this.newsFeed1.Spacing = this.newsFeed1.Height;
            this.newsFeed1.SetTexts();
            this.newsFeed1.startFeed();
        }

        private void SetupColors(Color[] colors)
        {
              if (this.newsFeed1.TextColor.Length > 0 && colors.Length > 0)
                   this.newsFeed1.TextColor[0] = colors[0];
               /*if (this.newsFeed1.TextColor.Length > 1 && colors.Length > 1)
                   this.newsFeed1.TextColor[1] = colors[1];
               if (this.newsFeed1.TextColor.Length > 2 && colors.Length > 2)
                   this.newsFeed1.TextColor[2] = colors[2];
               if (this.newsFeed1.TextColor.Length > 3 && colors.Length > 3)
                   this.newsFeed1.TextColor[3] = colors[3];
               if (this.newsFeed1.TextColor.Length > 4 && colors.Length > 4)
                   this.newsFeed1.TextColor[4] = colors[4];*/
        }

        private void SetupText(string[] textToDisplay)
        {
             if (this.newsFeed1.NewsTextFeed.Length > 0 && textToDisplay.Length > 0)
                  this.newsFeed1.NewsTextFeed[0] = textToDisplay[0];
              /*if (this.newsFeed1.NewsTextFeed.Length > 1 && textToDisplay.Length > 1)
                  this.newsFeed1.NewsTextFeed[1] = textToDisplay[1];
              if (this.newsFeed1.NewsTextFeed.Length > 2 && textToDisplay.Length > 2)
                  this.newsFeed1.NewsTextFeed[2] = textToDisplay[2];
              if (this.newsFeed1.NewsTextFeed.Length > 3 && textToDisplay.Length > 3)
                  this.newsFeed1.NewsTextFeed[3] = textToDisplay[3];
              if (this.newsFeed1.NewsTextFeed.Length > 4 && textToDisplay.Length > 4)
                  this.newsFeed1.NewsTextFeed[4] = textToDisplay[4];*/
        }
    }
}


Before using now the readableRss variable that is a string variable i use the SetupText and SetupColors like this:

private void Form1_Load(object sender, EventArgs e)
        {
            this.newsFeed1.NewsTextFeed = new string[5];
            this.newsFeed1.TextColor = new Color[5];
            SetupText(new string[] { "Hello everyone this is the weather for today", "Text2", "hello world this is a test for long text what do you think", "Text 4 -> 4", "Text Nr. 5" });
            SetupColors(new Color[] { Color.Blue, Color.Lime, Color.Maroon, Color.FromArgb(255, 71, 71, 255), Color.BurlyWood });
            //set this Variable to the Height of the Control to display one label at a time
            this.newsFeed1.Spacing = this.newsFeed1.Height;
            this.newsFeed1.SetTexts();
            this.newsFeed1.startFeed();
        }

        private void SetupColors(Color[] colors)
        {
              if (this.newsFeed1.TextColor.Length > 0 && colors.Length > 0)
                   this.newsFeed1.TextColor[0] = colors[0];
               if (this.newsFeed1.TextColor.Length > 1 && colors.Length > 1)
                   this.newsFeed1.TextColor[1] = colors[1];
               if (this.newsFeed1.TextColor.Length > 2 && colors.Length > 2)
                   this.newsFeed1.TextColor[2] = colors[2];
               if (this.newsFeed1.TextColor.Length > 3 && colors.Length > 3)
                   this.newsFeed1.TextColor[3] = colors[3];
               if (this.newsFeed1.TextColor.Length > 4 && colors.Length > 4)
                   this.newsFeed1.TextColor[4] = colors[4];
        }

        private void SetupText(string[] textToDisplay)
        {
             if (this.newsFeed1.NewsTextFeed.Length > 0 && textToDisplay.Length > 0)
                  this.newsFeed1.NewsTextFeed[0] = textToDisplay[0];
              if (this.newsFeed1.NewsTextFeed.Length > 1 && textToDisplay.Length > 1)
                  this.newsFeed1.NewsTextFeed[1] = textToDisplay[1];
              if (this.newsFeed1.NewsTextFeed.Length > 2 && textToDisplay.Length > 2)
                  this.newsFeed1.NewsTextFeed[2] = textToDisplay[2];
              if (this.newsFeed1.NewsTextFeed.Length > 3 && textToDisplay.Length > 3)
                  this.newsFeed1.NewsTextFeed[3] = textToDisplay[3];
              if (this.newsFeed1.NewsTextFeed.Length > 4 && textToDisplay.Length > 4)
                  this.newsFeed1.NewsTextFeed[4] = textToDisplay[4];
        }

And it was working good i saw the text in the SetupText moving in animation scroll from the bottom to up. Like a moving text scrolling automatic.

Now i wanted to make the text in readableRss to move the same way. But it dosent work.

The readableRss format text content looks like for example:

אם הרב אלקנה אליאסי נרצח ברקע לאומני?
Mon, 27 Jan 2014 10:27:40 +0200

ביל גייטס הובס תוך דקה ע''י צעיר בן 23 במשחק שחמט
Mon, 27 Jan 2014 10:25:30 +0200

Thats mean for example this two lines:

אם הרב אלקנה אליאסי נרצח ברקע לאומני?
Mon, 27 Jan 2014 10:27:40 +0200

Should move togeather like one block of text from bottom to up.

This is the code of the UserControl i did:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Speech.Synthesis;

namespace RssNews
{
    public partial class NewsFeed : UserControl
    {
        SpeechSynthesizer speaker;
        Label[] list = new Label[5];
        int jump = 0;

        public string[] NewsTextFeed { get; set; }
        public Color[] TextColor { get; set; }

        private int _spacing = 10;
        public int Spacing
        {
            get { return _spacing; }
            set { _spacing = value; }
        }

        public NewsFeed()
        {
            InitializeComponent();

            speaker = new SpeechSynthesizer();
            Init();
        }

        private void Init()
        {
            // Initialise Position Variables 
            int x = 2;
            int y = this.Height; // just out of view at top. 

            // Assign each label in list the following properties 
            for (int i = 0; i < 5; i++)
            {
                list[i] = new Label();
                list[i].AutoSize = false;
                list[i].Location = new Point(x, y + (_spacing * i)); // The math is the UserControl size for example 150x150 and then 150/the number of labels for example 150/5=30 so it will be 30 * i \\
                this.Controls.Add(list[i]); // Add control to UserControl 
            }
            startFeed();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            foreach (Label label in list)
            // Loop through List with for-each 
            {
                // Move label up 'jump' pixels 
                label.Location = new Point(label.Location.X, label.Location.Y - jump);

                // Check to see if out of view. 
                if (label.Location.Y <= -_spacing)
                {
                    //calculate the new Y-Position by summing all heights + the sum of _spacing - 1 time spacing, cause it is now at the ypoint -spacing
                    int newPosY = list.Sum((b) => (b.Height));
                    label.Location = new Point(label.Location.X, newPosY + _spacing * (list.Length - 1));
                }
            }
        }

        public void SetTexts()
        {
            if (list.Length > 4 && NewsTextFeed != null)
            {
                //get the label at this position in the list
                //and assign some text
                if (list[0] != null && this.NewsTextFeed[0] != null && NewsTextFeed.Length > 0)
                    list[0].Text = this.NewsTextFeed[0];
                //  speaker.SpeakAsync(list[0].Text); // this is where we make the text to speech...to add option for it yes or not using the option in form1.
                if (list[1] != null && this.NewsTextFeed[1] != null && NewsTextFeed.Length > 1)
                    list[1].Text = this.NewsTextFeed[1];

                if (list[2] != null && this.NewsTextFeed[2] != null && NewsTextFeed.Length > 2)
                    list[2].Text = this.NewsTextFeed[2];

                if (list[3] != null && this.NewsTextFeed[3] != null && NewsTextFeed.Length > 3)
                    list[3].Text = this.NewsTextFeed[3];

                if (list[4] != null && this.NewsTextFeed[4] != null && NewsTextFeed.Length > 4)
                    list[4].Text = this.NewsTextFeed[4];
            }

            if (list.Length > 4 && TextColor != null)
            {
                if (list[0] != null && TextColor.Length > 0 && !TextColor[0].Equals(Color.Transparent))
                    list[0].ForeColor = TextColor[0];

                if (list[1] != null && TextColor.Length > 1 && !TextColor[1].Equals(Color.Transparent))
                    list[1].ForeColor = TextColor[1];

                if (list[2] != null && TextColor.Length > 2 && !TextColor[2].Equals(Color.Transparent))
                    list[2].ForeColor = TextColor[2];

                if (list[3] != null && TextColor.Length > 3 && !TextColor[3].Equals(Color.Transparent))
                    list[3].ForeColor = TextColor[3];

                if (list[4] != null && TextColor.Length > 4 && !TextColor[4].Equals(Color.Transparent))
                    list[4].ForeColor = TextColor[4];
            }

            CheckTexts();
        }

        private void CheckTexts()
        {
            Label prev = null;
            foreach (Label l in this.list)
            {
                if (l != null)
                {
                    using (Graphics g = l.CreateGraphics())
                    {
                        SizeF sz = g.MeasureString(l.Text, l.Font);

                        int linesNeeded = (int)Math.Ceiling(sz.Width) / l.Width + 1;
                        l.Height = (int)Math.Ceiling(sz.Height * linesNeeded);

                        if (prev != null)
                            if (l.ClientRectangle.IntersectsWith(prev.ClientRectangle))
                                l.Location = new Point(l.Location.X, prev.Location.Y + prev.Height + _spacing);

                        prev = l;
                    }
                }
            }
        }

        public void startFeed()
        {
            try
            {
                {
                    // Set movement variables 
                    timer1.Interval = 50;
                    jump = 1;
                    // Start Timer 
                    timer1.Start();
                }
            }
            catch (Exception err)
            {
                MessageBox.Show("NewsFeeder Error: " + err);
            }
        }
    }
}

Its like a feeder that get text and animate it from bottom to top.

The problem is i dont know how to make it with the text in readableRss variable and using only one color for example Blue.

In the original i used 5 different texts and 5 different colors. But now i want to use readableRss and that have each time a different number of lines of text.

 

Viewing all articles
Browse latest Browse all 12583

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>