Carla Fenech Carla Fenech - 1 month ago 7
C# Question

C# get Label and change text

I am trying to loop through a list of currencies that I have and change the value of their labels.
I am looping through my currencies but how can I get the label and change its text of each label per currency as per example below.

EUR: 3.5
GBP 1.5
USD 2.5

lbl'item.currency'.Text = rate;

foreach (Currency item in new currencies().getAllCurr())
{

}


Thanks

mww mww
Answer

You can add controls (Label) with id = Currency.Name (id="EUR"). In loop you can use Controls.Find and find this control dynamically.

public class Currency
{
   public string Name {get; set;}
   public decimal Value {get; set;}
}

foreach (List<Currency> item in new currencies().getAllCurr())
        {
          Label tbx = this.Controls.Find(item.Name, true).FirstOrDefault() as Label;
           tbx.text = item.Value;
        }