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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Overriding TreeNode

Status
Not open for further replies.

mas3399

Programmer
Joined
Apr 5, 2005
Messages
3
Location
US
Hi,

I'm trying to create a custom TreeNode or add an (int) key to the TreeNode class. I want this to be used by the TreeView control

I've written a class derived from TreeNode, but don't know how to use it with the TreeView control so that I can access this key value.

Can anyone help me or point me in the right direction
 
VB Syntax:
Along the lines of
Dim ANode as New MyNode
ANode.etc = whatever
etc
etc
tree.Nodes.Add(ANode)

When accessing the node you will have access to its properties:

eg

ANode = CType(tree.SelectedNode, MyNode)
ANode.AnyAvailablePropertyFromNodeOrMyNode etc


By the way, if you are currently not using the Tag property of the normal treenode, then maybe you could use that instead.
 
Ive just done this exact same thing, when using it you need to cast it as your special type of node, so that you can access your custom members.

For example, say your have an interger called "myInt" added to the Node, in your "myNode" class, to access this, say, in an event of the TreeView, you would do something like this:
Code:
myTreeView_MouseUp(object sender, MouseEventArgs e)
{
   myNode tempNode;
   tempNode = (myNode)myTreeView;

   tempNode.myInt++;  // Or whatever you want to do with it.

}
 
damn why cant we edit posts here??

the 4th line should be this:
tempNode = (myNode)myTreeView.SelectedNode;
 
After posting this, I did some reading and I decided to develop a NodeInformation Object for the Tag field instead, which will carry primary keys and is a much easier solution.

Thanks for your information, however, as I'll tuck it away for future reference.

 
Unfortunately my C(++/#) is not really good enough for me to risk an example, hence the VB syntax.

By the way, if you click preview post, then you get an Edit Post & a Submit Post button
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top