Extending the treeview control

  • Thread starter Patrick McGuire
  • Start date
P

Patrick McGuire

Article 311318, "HOW TO: Create a Key Property for a
TreeView Node in Visual Basic .NET". Explains how to
extend the treeview (and treenode) classes to include a
node Key. However, to obtain a node reference, you must
still either use the index, or use a for each ... next
loop in you code to get it by key. I would like to use
the node's key or index interchangeably. To do this, I
think I need to change the treeview's Nodes property from
type TreeNodeCollection to a user defined type
TreeNodeExCollection, inheriting everything from
TreeNodeCollection but the item property. How do I do
this?

When I try, I am forced to write my own New sub, and am
not allowed to access MyBase.New(owner as treenode),
because it is private.
 
N

NoLongerMicah

Yep - as far as I know you can not inherit from the TreeNodeCollection
class since the constructor is private. Therefore, I believe this
makes the class sealed (non-derivable).

My suggestion is that you create a new class that implements the IList
interface. However, there are some things that make this very
difficult and icky, (see my post on the C# board at
http://www.dotnetboards.com/viewtopic.php?t=16350)

If you found any way round this private constructor then please let me
know.
 
N

NoLongerMicah

This is the way I got around the problem (In C#). I'm not thrilled
that it's the best solution, but it does work...

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();
}
...
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top