2017-07-15 49 views
0

我有一個數組標籤名稱標題[i]和link [i]。在事件處理程序中讀取標籤數組信息

Label[] title = new Label[100]; 
Label[] link = new Label[100]; 
for (int i = 0; i < 10; i++) 
{ 
title[i] = new Label(); 
link[i] = new Label(); 
} 

當我點擊標籤標題時,我也可以獲取鏈接標籤信息。

title[i].MouseClick += new EventHandler(hover_title); 

我試試這個代碼不工作。

public void hover_title(object sender, EventArgs e) 
      { 
       title[i].text=link[i].text; 
      } 

當我點擊標題標籤時,如何獲得標籤鏈接文本。

+1

是什麼意思** 「我用發件人的更改標題[I]」 **,什麼是** 「鏈接[I]」 **你,我們我一直在談論**「title [i]」**。對不起,但這個問題很不明確。請更新。 – Waescher

+0

粘貼代碼中聲明「標題」和「鏈接」數組的地方的一部分; – Bahrom

回答

1

像下面的東西應該解決您的問題。

public void hover_title(object sender, EventArgs e) 
    { 
     var label = sender as Label; 
     int i = (title as IList).IndexOf(label); 
     label.Text = link[i].Text; 
    } 

記住,你創建一個控件後,你必須給它一個新的位置,在標籤的情況下,設置文本,新的大小(以標籤的情況下,你可以設置AutoSize屬性爲true),並添加它到父控制的Controls集合。

+0

出現錯誤,(使用泛型類型'IList '需要1個類型參數。) – Sebachtian

+1

我使用System.Collections添加了命名空間「 – Bahrom

+0

啊忘了使用System.Collections,感謝兄弟 – Sebachtian

1

你可以這樣做:

Label[] title = new Label[100]; 
Label[] link = new Label[100]; 
for (int i = 0; i < 10; i++) 
{ 
    var j = i; 
    title[j] = new Label(); 
    link[j] = new Label(); 
    title[j].MouseClick += (s, e) => title[j].Text = link[j].Text; 
} 
+1

更好。 +1。 – Bahrom