Lavi
Lavi

Reputation: 194

Display strings from a list one by one into a label

I have a label. I have a list. when I do "label1.Text = match.Value;", it just displays the last item of the list, as opposed to 1 string that changes each time I click a button. The code is:

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)*/ 
        }

    }

Thanks in advance!

Upvotes: 0

Views: 7753

Answers (4)

shary.sharath
shary.sharath

Reputation: 709

In Second foreach loop put label1.Text += title; instead of label1.Text = title; then it will work fine.

Upvotes: 0

Mark Byers
Mark Byers

Reputation: 839114

You need to initialize the list outside of your click event handler. You could create an FetchImageData method that is called when your program starts (perhaps call it from the constructor of your class). Or you could call it the list the first time the click event is fired.

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);
    }
}

You haven't said what should happen when the user clicks more times than there are elements. One option is to wrap around and starts again from the beginning. This can be achieved using the % operator.

private void frontPageToolStripMenuItem_Click(object sender, EventArgs e)
{
    if (titles == null) { FetchImageData(); }
    label1.Text = titles[clickCounter % titles.Count];
    clickCounter++;
}

Upvotes: 4

wxyz
wxyz

Reputation: 707

1) titles should be global variable

2) Move this in the constructor of the form

    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

3) Your event handler should be like this:

private void frontPageToolStripMenuItem_Click(object sender, EventArgs e)
    {
            label1.Text = next value of the titles variable;

    }

Upvotes: 0

DjSol
DjSol

Reputation: 208

UI thread doesn't get a chance to update the label because your for loop is on the UI thread. Even if you move the for loop to a background thread good chance you 'll only see some flickering and then the final string. Just append the strings to a textbox or output them using Debug.writeLine. This will show you that you are reading the correct strings.

To change the label just once don't do it in a for loop

Upvotes: 0

Related Questions