2015-08-09 47 views
0

我在代碼中動態生成一對treeview控件,並將所有treeview指向同一個SelectedNodeChanged事件。但是,在這種情況下,我無法獲取包含被單擊節點的特定樹視圖。從EventArgs中獲取特定的樹視圖

for (int i = 0; i < numTree; i++) 
{ 
    treeList[i] = new TreeView(); 
    treeList[i].SelectedNodeChanged += TreeSelectedNodeChanged; 
} 

private void TreeSelectedNodeChanged(object sender, EventArgs e) 
{ 
    // cannot call treeList[i] since all treeview should direct to this event 
    Message.Text = "You selected: " + treeList[i].SelectedNode.Text; 
} 

如有任何意見,將不勝感激!

回答

0

sender參數是控制觸發事件

private void TreeSelectedNodeChanged(object sender, EventArgs e) 
{ 
    var selected_tree_view = (TreeView)sender; 
    // Just in case you need it 
    var selected_tree_view_index = treeList.IndexOf(selected_tree_view); 
    Message.Text = "You selected: " + selected_tree_view.SelectedNode.Text; 
} 
+0

行之有效!非常感謝! – user3081703