2016-11-08 136 views
2

我試圖循環訪問我已有的貨幣列表並更改其標籤的值。 我正在循環使用我的貨幣,但如何獲取標籤並更改每個貨幣的每個標籤的文本,如下例所示。C#獲取標籤並更改文本

EUR:3.5 GBP 1.5 USD 2.5

lbl'item.currency'.Text =速率;

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

     } 

感謝

+0

LabelControlNAme.Text =「Your value」; – mybirthname

+1

我猜你有多個標籤(每個貨幣類型都有一個標籤)你打算如何將適當的貨幣連接到指定的標籤?顯示** Currency **類的結構將有很大的幫助,我猜它是由字符串類型**名稱**和雙重類型**值**字段組成的? – Innat3

回答

2

你可以用id = Currency.Name(ID = 「EUR」)添加控件(標籤)。在循環中,您可以使用Controls.Find並動態查找此控件。

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; 
     } 
+1

標籤不是文本框 –