Label in loop on Foreach

Asked

Viewed 95 times

3

I’m having difficulty showing information that comes from an object to display on a Label, returning in list form.

//Pegando os dados do Rest e armazenando na variável usuários

var usuario = response.Content.ReadAsAsync<IEnumerable<ConsumeRoot>>().Result;

foreach (var a in usuario)
{
    lblTitulo.Text = a.titulo;
}

The label returns only one information, this class is receiving an API that is receiving information from a database, are 6 information that is to appear in this label informs stack.

  • In addition to the allocation operator error, Label is not recommended to show lists, a control like BulletedList (web Forms), ListBox (win Forms) or ListView (Xamarin) would be more appropriate

  • Young man, I realized that you are trying to accept both answers as correct. You need to choose only one to score. If the two helped you, you can vote positively on both, but mark as correct only one.

2 answers

7

It turns out that the label value is being replaced with every noose.

Concatenate the values or use string.Join and do only one assignment

var usuario = response.Content.ReadAsAsync<IEnumerable<ConsumeRoot>>().Result;

lblTitulo.Text = string.Join(", ", usuario.Select(u => u.Titulo));

5


In each iteration of this foreach, you are replacing the previous information, maybe you are forgetting to concatenate the results:

    foreach (var a in usuario)
    {
        lblTitulo.Text += a.titulo + " ";
    }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.