David Asssad
David Asssad

Reputation: 41

How to load an array in labels?

I have an array that retrieves data from a csv file.

 String[] array = File.ReadAllText(@"c:\\arrayexample.csv").Split(';');

This array is now filled till array[10]. What is the best way to load data in the array to labels? For example, can I load all the data in generated labels:

label1  // array[0]
label2  // array[1]
label3  // array[2]
..and so on.

I can do it manually by adding the labels myself but is there a way to program it? Also, will this work using System.IO?

label1.AppendText(array[i]);

Thanks!

Upvotes: 1

Views: 2999

Answers (2)

Chris Dixon
Chris Dixon

Reputation: 9167

foreach(String labelText in array)
{
    Label label = new Label();
    label.Text(labelText);
    // Do whatever you want with each label here. i.e. add to your form.
}

Upvotes: 2

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 727137

Assuming that there is a one-to-one correspondence between your labels and the strings in the array, you can put these labels in an array, and then set the data in them using a loop, like this:

var labels = new[] {label1, label2, label3, ...};
for (var i = 0 ; i != array.Length ; i++) {
    labels[i].Text = array[i];
}

Upvotes: 4

Related Questions