Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Creating a new TreeNodeCollection (and getting handles,...) 1

Status
Not open for further replies.

tenshekels

Programmer
Feb 5, 2004
20
US
I have derived new classes for the TreeNode and TreeView classes that Microsoft provides. Since the TreeNodeCollection class is sealed I created my own class that implements the IList interface.

However, the original TreeNodeCollection class does something important on the "Add" method where it assigns the "TreeView" and "Handle" to the TreeNode object that you are adding. I need this information - and can not figure out how they are doing this. Needless to say - my class that I created does not do this.

Does anyone have some code for Microsoft's implementation of the TreeNodeCollection class - or does anyone have any idea of where I should start?

It is very annoying that they sealed this class (with internal constructor)...
 
Even if you have the code for the TreeNodeCollection class you will need more and more code for another classes!
I see a solution to use the existing functionality of the TreeNodeCollection by having a member with this type in your class and add there your new features:
public class MyTreeNodeCollection
{
private TreeNodeCollection tnc;
// other features
public MyTreeNodeCollection()
{
// ...
}
}
}
-obislavu-
 
Hey, thanks for the help obislavu.

However - this will not work. Since the constructor is internal I cannot create an instance of the TreeNodeCollection class (on heap - using "new" keyword). Therefore, if I tried your solution and tried accessing the "tnc"'s methods - I would get the error: "Object reference not set to an instance of an object.".

Any other ideas?
 
I understood you will use like here:

public class Class1
{
private TreeView tv;
private TreeNodeCollection tnc;
public Class1()
{
tv = new TreeView();
TreeNode tn = new TreeNode("abc");
tnc = tv.Nodes;
//
// TODO: Add constructor logic here
//
}
}
-obislavu-
 
Thanks again for your input!

I changed your scenario around a bit to get something that works for me...

public class ICNodeCollection
{
private TreeNodeCollection nodes;

public ICNodeCollection(TreeNodeCollection treeNodes)
{
nodes = treeNodes;
}
.... (add special functions for NodeCollection class)
}

public class ICTreeNode : System.Windows.Forms.TreeNode
{
private ICNodeCollection nodes;

public ICTreeNode() : base()
{
nodes = new ICNodeCollection(base.Nodes);
}

....
}

public class ICTreeView : System.Windows.Forms.TreeView
{
private ICNodeCollection nodes;

public ICTreeView() : base()
{
nodes = new ICNodeCollection(base.Nodes);
InitializeTreeView();
}
...
}

I am still annoyed that Microsoft did not allow me to derive from the class - but this should work quite well.
Thanks again.
 
I think this is a good think and I am sure you will be in a situation to do the same with some of your classes.
-obislavu-
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top