2011-08-17 34 views
0

我試圖爲XML文檔中的每個XML元素加載一個新按鈕。我需要爲文檔中的每個xml節點創建一個新的按鈕實例。下面的代碼儘可能使新按鈕變爲可用,但該按鈕具有一個名爲「money」的自定義屬性,該屬性將不斷被覆蓋。所以我試圖找到一些方法來動態命名按鈕,E.G.按鈕(動態名稱)=新建按鈕();動態加載XML中的新按鈕

int x = 0; 
int y = 0; 
XmlDocument xmlDoc= new XmlDocument(); //* create an xml document object. 
xmlDoc.Load("XML.xml"); 
XmlNodeList elemList = xmlDoc.GetElementsByTagName("item"); 
for (int i = 0; i < elemList.Count; i++) 
{ 
    string money = elemList[i].Attributes["www"].Value; 
    string itemName = elemList[i].Attributes["money"].Value; 

    button LB = new button(); 
    LB.Text = money + itemName; 
    LB.money = itemName; 
    LB.Location = new System.Drawing.Point(x, y); 
    x += 25; 
    y += 25; 
    LB.Click += new EventHandler(item_click); 
    this.Controls.Add(LB); 
} 
+0

是'button'您的自定義類從'System.Windows.Forms.Button'獲得?誰重寫'錢',爲什麼?如何設置名稱幫助?你有沒有嘗試設置'名稱'屬性? – svick

回答

0

嘗試

button LB = new button(); 
LB.Text = money + itemName; 
string tmp_itemName = itemName; 
LB.money = tmp_itemName; 

只要.money也不是一成不變的這個作品......

+0

哈,它是一個靜態字符串。它現在效果很好:)謝謝 – craigbett