这是代码:
readableRss = RssReader.covertRss("http://rotter.net/rss/rotternews.xml");
string[] lines = Regex.Split(readableRss, "\r\n");
this.newsFeed1.NewsTextFeed = new string[5];
for (int i = 0; i < lines.Length; i++)
{
counter += 1;
if (counter <= 5)
{
lines[i] = "hi";
SetupText(new string[] { lines[0],lines[1] });
}
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();
在变量行中,我有151行。每行包含一个字符串,例如:
“大家好,今天的天气是......”
我有这句话:
SetupText(new string[] { lines[0],lines[1] });
使用FOR循环遍历并将每行添加到SetupText。 但是我如何使用循环添加所有行,而不是像我一样逐个添加它们? 我现在不会写行[0]行[1] .....行[151]
如果我改变它:
lines[i] = "hi";
SetupText(new string[] { lines[i] });
然后我只看到“嗨”一次。 但我没有看到所有其他行,因为lines [i]只是SetupText中的一个地方
如果我正在做:
lines[i] = "hi";
SetupText(new string[] { lines[i],lines[i] });
我会两次看到“嗨”。 如果我删除该行:
lines[i] = "hi";
我什么也看不见。 我需要SetupText将包含变量行中的所有行。
编辑:
这是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[5];
for (int i = 0; i < lines.Length; i++)
{
counter += 1;
if (counter <= 5)
{
lines[i] = "hi";
SetupText(new string[] { lines[i],lines[i] });
}
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];
}
}
}
我使用NewsFeed UserControl为内部文本设置动画,使其从下往上移动。
编辑**
这是用户控件NewsFeed.cs
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);
}
}
}
}
我刚刚将我的项目添加到我的SkyDrive中,也许有人可以下载它,看看我的意思?现在程序我将其设置回使用其原始设置,以便您可以看到它的工作原理如何工作。我想要做的是使用我标记为不立即使用的变量readableRss中的行,以便您可以看到该程序如何工作。
文件名是RssNews.rar
编辑**
这就是我现在正在尝试的事情:
private void Form1_Load(object sender, EventArgs e)
{
const int maxLines = 5;
string[] lines = Regex.Split(readableRss, "\r\n");
this.newsFeed1.NewsTextFeed = new string[maxLines];
SetupText(lines.Skip(Math.Max(0, collection.Count() - maxLines)).Take(maxLines).ToArray());
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();
}
但我在线上遇到错误:
lines.Skip(Math.Max(0, collection.Count() - maxLines))
错误5'string []'不包含'Skip'的定义,并且最好的扩展方法重载'System.Linq.Enumerable.Skip(System.Collections.Generic.IEnumerable,int)'有一些无效的参数< / p>
错误6参数2:无法从'decimal'转换为'int'
错误4当前上下文中不存在名称“collection”
编辑**
修复了错误,但现在我在UserControl中看不到任何内容,这是代码:
private void Form1_Load(object sender, EventArgs e)
{
const int maxLines = 5;
readableRss = RssReader.covertRss("http://rotter.net/rss/rotternews.xml");
string[] lines = Regex.Split(readableRss, "\r\n");
this.newsFeed1.NewsTextFeed = new string[maxLines];
SetupText(lines.Skip(Math.Max(0, readableRss.Length - maxLines)).Take(maxLines).ToArray());
this.newsFeed1.TextColor = new Color[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 Form1_Load(object sender, EventArgs e)
{
readableRss = RssReader.covertRss("http://rotter.net/rss/rotternews.xml");
const int maxLines = 5;
string[] lines = Regex.Split(readableRss, "\r\n")
.Where(str => !string.IsNullOrEmpty(str))
.ToArray();
this.newsFeed1.NewsTextFeed = new string[maxLines];
SetupText(lines
.Skip(Math.Max(0, readableRss.Length - maxLines))
.Take(maxLines)
.ToArray());
不工作我什么都看不见。
编辑**
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;
using System.Collections;
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");
const int maxLines = 5;
string[] lines = Regex.Split(readableRss, "\r\n")
.Where(str => !string.IsNullOrEmpty(str))
.ToArray();
this.newsFeed1.NewsTextFeed = new string[maxLines];
SetupText(lines
.Skip(Math.Max(0, readableRss.Length - maxLines))
.Take(maxLines)
.ToArray());
/*const int maxLines = 5;
string[] lines = Regex.Split(readableRss, "\r\n");
this.newsFeed1.NewsTextFeed = new string[maxLines];
SetupText(lines.Skip(Math.Max(0, readableRss.Length - maxLines)).Take(maxLines).ToArray());*/
this.newsFeed1.TextColor = new Color[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];
}
}
}
答案 0 :(得分:1)
看起来您的控件一次只能显示5个项目,因此完全没必要循环遍历所有项目数组并逐个传递(或5乘5)。将list [i]设置为“my”也会在最后用“hi”覆盖所有数组。
所以我会用这样的东西:
const int maxLines = 5;
var lines = Regex.Split(readableRss, "\r\n")
.Where(str => !string.IsNullOrEmpty(str))
.ToList();
this.newsFeed1.NewsTextFeed = new string[maxLines];
SetupText(lines
.Skip(Math.Max(0, lines.Count() - maxLines))
.Take(maxLines)
.ToArray());
答案 1 :(得分:-2)
也许你正在寻找String.Join。
http://msdn.microsoft.com/en-us/library/System.String.Join(v=vs.110).aspx