Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将列表中的字符串逐个显示到标签中_C#_List_Loops - Fatal编程技术网

C# 将列表中的字符串逐个显示到标签中

C# 将列表中的字符串逐个显示到标签中,c#,list,loops,C#,List,Loops,我有一个标签。我有一张单子。当我执行“label1.Text=match.Value;”时,它只显示列表的最后一项,而不是每次单击按钮时更改的一个字符串。代码是: private void frontPageToolStripMenuItem_Click(object sender, EventArgs e) { const string url = "http://reddit.com/r/pics"; var source = getSource(ur

我有一个标签。我有一张单子。当我执行“label1.Text=match.Value;”时,它只显示列表的最后一项,而不是每次单击按钮时更改的一个字符串。代码是:

private void frontPageToolStripMenuItem_Click(object sender, EventArgs e)
    {
        const string url = "http://reddit.com/r/pics";
        var source = getSource(url);
        var regex = new Regex([regex removed]);
        var links = new List<string>();
        var titles = new List<string>();

        foreach (Match match in regex.Matches(source))
        {
            links.Add(match.Groups[1].Value);
            titles.Add(match.Groups[2].Value);

        }

        foreach (var title in titles)
        {
            label1.Text = title; /*it just shows the last 'title' in 'titles', I want it to start at the first, and go to the next title every time the event occurs (frontPageToolStripMenuItem_Click)*/ 
        }

    }
private void frontPageToolStripMenuItem\u单击(对象发送方,事件参数e)
{
常量字符串url=”http://reddit.com/r/pics";
var source=getSource(url);
var regex=新的regex([regex已删除]);
var links=新列表();
var titles=新列表();
foreach(regex.Matches中的匹配(源))
{
links.Add(match.Groups[1].Value);
titles.Add(match.Groups[2].Value);
}
foreach(标题中的var标题)
{
label1.Text=title;/*它只显示“标题”中的最后一个“标题”,我希望它从第一个开始,并在每次事件发生时转到下一个标题(frontPageToolStripMenuItem_单击)*/
}
}

提前谢谢

您需要在单击事件处理程序之外初始化列表。您可以创建一个
FetchImageData
方法,该方法在程序启动时调用(可能从类的构造函数调用)。或者,您可以在首次触发单击事件时将其称为列表

private int clickCounter = 0;
private List<string> links;
private List<string> titles;

private void FetchImageData()
{
    links = new List<string>();
    titles = new List<string>();

    const string url = "http://reddit.com/r/pics";
    var source = getSource(url);
    var regex = new Regex([regex removed]);

    foreach (Match match in regex.Matches(source))
    {
        links.Add(match.Groups[1].Value);
        titles.Add(match.Groups[2].Value);
    }
}

您需要在单击事件处理程序之外初始化列表。您可以创建一个
FetchImageData
方法,该方法在程序启动时调用(可能从类的构造函数调用)。或者,您可以在首次触发单击事件时将其称为列表

private int clickCounter = 0;
private List<string> links;
private List<string> titles;

private void FetchImageData()
{
    links = new List<string>();
    titles = new List<string>();

    const string url = "http://reddit.com/r/pics";
    var source = getSource(url);
    var regex = new Regex([regex removed]);

    foreach (Match match in regex.Matches(source))
    {
        links.Add(match.Groups[1].Value);
        titles.Add(match.Groups[2].Value);
    }
}

UI线程没有机会更新标签,因为for循环位于UI线程上。即使将for循环移动到背景线程,也很有可能只会看到一些闪烁,然后看到最后的字符串。只需将字符串附加到文本框或使用Debug.writeLine输出它们。这将显示您正在读取正确的字符串


要仅更改一次标签,请不要在for循环中执行此操作,UI线程没有机会更新标签,因为for循环位于UI线程上。即使将for循环移动到背景线程,也很有可能只会看到一些闪烁,然后看到最后的字符串。只需将字符串附加到文本框或使用Debug.writeLine输出它们。这将显示您正在读取正确的字符串

要仅更改一次标签,请不要在for循环中执行此操作。
标题应为全局变量

2) 在窗体的构造函数中移动此项

    const string url = "http://reddit.com/r/pics";
                var source = getSource(url);
                var regex = new Regex("<a class=\"title \" href=\"(.*?)\" >(.*?)<");

        titles = new List<string>();

                foreach (Match match in regex.Matches(source))
                {
                    links.Add(match.Groups[1].Value);
                    titles.Add(match.Groups[2].Value);

                }
label1.Text = first value of the titles
1)
标题
应为全局变量

2) 在窗体的构造函数中移动此项

    const string url = "http://reddit.com/r/pics";
                var source = getSource(url);
                var regex = new Regex("<a class=\"title \" href=\"(.*?)\" >(.*?)<");

        titles = new List<string>();

                foreach (Match match in regex.Matches(source))
                {
                    links.Add(match.Groups[1].Value);
                    titles.Add(match.Groups[2].Value);

                }
label1.Text = first value of the titles

在第二个
foreach
循环中放入
标签1.Text+=title
而不是
label1.Text=标题
然后它就可以正常工作了。

在第二个
foreach
循环放置
label1.Text+=title
而不是
label1.Text=标题然后它就可以正常工作。

标题应该是一个全局变量,您应该在加载表单时构建它。在click事件处理程序中,您应该只将标题移动到下一个值。所有代码都将一次执行。对于每个按钮单击,
foreach
循环没有理由只按一个元素进行,那么为什么要这样做呢?相反,它们迭代整个枚举,因此保留最后一个元素。如果您想一步一步地进行,您必须将当前进度保存在方法之外的某个位置。titles应该是一个全局变量,您应该在加载表单时构建它。在click事件处理程序中,您应该只将标题移动到下一个值。所有代码都将一次执行。对于每个按钮单击,
foreach
循环没有理由只按一个元素进行,那么为什么要这样做呢?相反,它们迭代整个枚举,因此保留最后一个元素。如果你想一步一步地进行,你必须将当前进度保存在你的方法之外的某个地方。+1模是一个非常好的解决方案。我认为只需要检查计数>0。@MarkByers:已经+1'了,并删除了我的注释,因为它不再适用。+1模是一个非常好的解决方案。我认为只需要检查计数>0。@MarkByers:已经+1'并删除了我的注释,因为它不再适用。