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
Loading
anirudh sarafPosted Oct 21, 2007, 8:07 PM
Thanks for the response. It works like a charm :).
Thanks,
Anirudh
AlanPosted Oct 21, 2007, 7:03 PM
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
}