Hi,
I am new to VC # and I am stuck as to how to create an array of objects like the treeview programmatically.
After I create it programmatically using the new statement, how do I write events for it and figure out which index of the control raised the event?
treeNB[i] = new System.Windows.Forms.TreeView();
how do I create events for the treeNB object and then use some sort of index property?
Thanks a lot for the help.
Anirudh
Create a array of Control and raise events
2 Answers
Know the answer? Post it. Somebody with the same question will find it here.

AlanRank #669 · 3.6K reputationPosted Oct 21, 2007, 3:03 PM EDT
I'd start by giving each TreeView a name corresponding to its index in the array and linking them all up to the same eventhandler, like this:
for (int i = 0; i < numTreeViews; i++)
{
treeNB[i] = new System.Windows.Forms.TreeView();
treeNB[i].Name = "treeNB" + i.ToString();
treeNB[i].AfterSelect += new TreeviewEventHandler(TreeNB_AfterSelect);
}
You can then figure out which Treeview caused the event like this:
private void TreeNB_AfterSelect(System.Object sender,
System.Windows.Forms.TreeViewEventArgs e)
{
TreeView treeNB = (TreeView)sender;
int tbIndex = int.Parse(treeNB.Name.Substring(6));
// so treeNB[tbIndex] caused the event
// other code
}
anirudh saraf5 reputationPosted Oct 21, 2007, 4:07 PM EDT
Thanks for the response. It works like a charm :).
Thanks,
Anirudh