我有这个form1的代码:
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 = "";
int counter;
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++)
{
counter += 1;
if (counter <= 5)
{
t = Convert.ToString(lines[i]);
SetupText(new string[] { t,t,t,t,t });
}
else
{
counter = 0;
}
this.newsFeed1.TextColor = new Color[5];
SetupColors(new Color[] { Color.Blue, Color.Lime, Color.Maroon, Color.FromArgb(255, 71, 71, 255), Color.BurlyWood });
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];
}
}
}
readableRss是一个字符串
当我进入SetupText时(新字符串[] { 变量t一次或5次不会在UserControl上显示任何内容。 但是如果对于测试我用“hi”切换第一个t然后我在UserControl中看到“hi”。
为什么在使用变量t时它没有显示任何东西,但是当我使用“hi”时呢? t也是一个字符串。
这是类NewsFeed(newsFeed1变量)的代码:
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);
}
}
}
}
一般来说,我的项目是将UserControl NewsFeed拖动到form1设计器,并在UserControl中将文本从底部移动到顶部。 在form1中的原始文件中,我在SetupText和SetupColors中使用了我自己的文本和颜色。
但现在我想使用变量readableRss中的文本。 这是readableRss的内容示例:
צפו: נפרד ממשפחתו ויוצא לבצע פיגוע התאבדות
Mon, 27 Jan 2014 19:18:39 +0200
סוף סוף: פורסמו הכרזים לבניה ברמת שלמה בי-ם
Mon, 27 Jan 2014 19:10:03 +0200
צפו בוידאו: משלחת של שרים וחכ''ים במחנה ההשמדה אושוויץ - השר אריאל אומר קדיש
Mon, 27 Jan 2014 19:09:25 +0200
所以例如readabelRss中的这部分内容:
סוף סוף: פורסמו הכרזים לבניה ברמת שלמה בי-ם
Mon, 27 Jan 2014 19:10:03 +0200
这两行应该是一个应该从底部移动到顶部的文本块。
现在这就是我在行中看到的:
在索引0中,我看到:“צפו:נפרדממשפחתוויוצאלבצעפיגועהתאבדות 在索引1中,我看到:“星期一,2014年1月27日19:18:39 +0200”
现在这种情况下有151行。
FOR循环中的变量t现在包含:
צפו: נפרד ממשפחתו ויוצא לבצע פיגוע התאבדות
在下一次迭代中,变量t包含:
Mon, 27 Jan 2014 19:18:39 +0200
我想要的是一行的每个文本以及它下面的下一行时间和日期将像动画一样从底部移动到顶部。 它使用我的代码,如果我使用自己的文本和颜色,但我想使用变量readableRss中的文本。
答案 0 :(得分:0)
由于使用文本hi
而不是实际的字符串值,唯一的可能性是ConvertRss
方法的输出仅返回换行符或为空。您应该检查readableRss
变量的值。
请注意,t = Convert.ToString(lines[i]);
无用,因为lines
是string
的数组。