TreeView Question

G

Guest

I have a treeView with nested Items, I need to set for each node in tree a
specific data like following

Root---
---Node1 (here i need to attach url for each item so when user click
it display it in DHtmlDialog)
---Node2
---Node3
so my question how to set this data for each item?
 
C

Carlos J. Quintero [VB MVP]

You can create a TreeNodeEx class which inherits from TreeNode and which
contains additional properties and then you add instances of it to the
treeview. When a TreeNode is clicked, you cast back to TreeNodeEx and
retrieve the additional data.

--

Best regards,

Carlos J. Quintero

MZ-Tools: Productivity add-ins for Visual Studio .NET, VB6, VB5 and VBA
You can code, design and document much faster.
Free resources for add-in developers:
http://www.mztools.com
 
G

Guest

ok im building the tree according to a level no [0-4] extracted from XMl fiel
as following


TreeNode tnMainNode = null;
TreeNode tnLevelOne = null;
TreeNode tnLevelTwo = null;
TreeNode tnLevelThree = null;
TreeNode tbLevelFour = null;

for(int iItem = 0 ; iItem < nItems ;iItem++)
{

if(levelNodes[iItem].InnerText.Equals("0"))
tnMainNode = tvTOC.Nodes.Add(descNodes[iItem].InnerText);
switch(levelNodes[iItem].InnerText)
{
case "1":
tnLevelOne = tnMainNode.Nodes.Add(descNodes[iItem].InnerText);
break;
case "2":
tnLevelTwo = tnLevelOne.Nodes.Add(descNodes[iItem].InnerText);
break;
case "3":
tnLevelThree = tnLevelTwo.Nodes.Add(descNodes[iItem].InnerText);
break;
case "4":
tbLevelFour = tnLevelThree.Nodes.Add(descNodes[iItem].InnerText);
break;
}
}

I created the custom node inherited from TreeNode and add my own data and
replace the TreeNode objects above with it to be

TreeNodeEx tnMainNode = null;
TreeNodeEx tnLevelOne = null;
TreeNodeEx tnLevelTwo = null;
TreeNodeEx tnLevelThree = null;
TreeNodeEx tbLevelFour = null;

but when add it return TreeNode when ..so cast it to TreeNodeEx...
exception said Specified cast is not valid...what's wrong?
 
C

Carlos J. Quintero [VB MVP]

The TreeNodeCollection class of the Nodes property has 2 overloaded Add
methods, and you are using the one that receives the node text (which
actually adds a TreeNode object). You need to use the other one, which
receives an instance already created of a TreeNode (or a derived class). So:

Instead of:

tnLevelOne = tnMainNode.Nodes.Add(descNodes[iItem].InnerText);

use:

treeNodeEx = New TreeNodeEx(descNodes[iItem].InnerText)
treeNodeEx.MyData = blah, blah
tnMainNode.Nodes.Add(treeNodeEx)

--

Best regards,

Carlos J. Quintero

MZ-Tools: Productivity add-ins for Visual Studio .NET, VB6, VB5 and VBA
You can code, design and document much faster.
Free resources for add-in developers:
http://www.mztools.com


Raed Sawalha said:
ok im building the tree according to a level no [0-4] extracted from XMl
fiel
as following


TreeNode tnMainNode = null;
TreeNode tnLevelOne = null;
TreeNode tnLevelTwo = null;
TreeNode tnLevelThree = null;
TreeNode tbLevelFour = null;

for(int iItem = 0 ; iItem < nItems ;iItem++)
{

if(levelNodes[iItem].InnerText.Equals("0"))
tnMainNode = tvTOC.Nodes.Add(descNodes[iItem].InnerText);
switch(levelNodes[iItem].InnerText)
{
case "1":
tnLevelOne = tnMainNode.Nodes.Add(descNodes[iItem].InnerText);
break;
case "2":
tnLevelTwo = tnLevelOne.Nodes.Add(descNodes[iItem].InnerText);
break;
case "3":
tnLevelThree = tnLevelTwo.Nodes.Add(descNodes[iItem].InnerText);
break;
case "4":
tbLevelFour = tnLevelThree.Nodes.Add(descNodes[iItem].InnerText);
break;
}
}

I created the custom node inherited from TreeNode and add my own data and
replace the TreeNode objects above with it to be

TreeNodeEx tnMainNode = null;
TreeNodeEx tnLevelOne = null;
TreeNodeEx tnLevelTwo = null;
TreeNodeEx tnLevelThree = null;
TreeNodeEx tbLevelFour = null;

but when add it return TreeNode when ..so cast it to TreeNodeEx...
exception said Specified cast is not valid...what's wrong?

Carlos J. Quintero said:
You can create a TreeNodeEx class which inherits from TreeNode and which
contains additional properties and then you add instances of it to the
treeview. When a TreeNode is clicked, you cast back to TreeNodeEx and
retrieve the additional data.
 
A

Alex Passos

You can use the TreeNode Tag property to set it to an object.

Carlos J. Quintero said:
The TreeNodeCollection class of the Nodes property has 2 overloaded Add
methods, and you are using the one that receives the node text (which
actually adds a TreeNode object). You need to use the other one, which
receives an instance already created of a TreeNode (or a derived class).
So:

Instead of:

tnLevelOne = tnMainNode.Nodes.Add(descNodes[iItem].InnerText);

use:

treeNodeEx = New TreeNodeEx(descNodes[iItem].InnerText)
treeNodeEx.MyData = blah, blah
tnMainNode.Nodes.Add(treeNodeEx)

--

Best regards,

Carlos J. Quintero

MZ-Tools: Productivity add-ins for Visual Studio .NET, VB6, VB5 and VBA
You can code, design and document much faster.
Free resources for add-in developers:
http://www.mztools.com


Raed Sawalha said:
ok im building the tree according to a level no [0-4] extracted from XMl
fiel
as following


TreeNode tnMainNode = null;
TreeNode tnLevelOne = null;
TreeNode tnLevelTwo = null;
TreeNode tnLevelThree = null;
TreeNode tbLevelFour = null;

for(int iItem = 0 ; iItem < nItems ;iItem++)
{

if(levelNodes[iItem].InnerText.Equals("0"))
tnMainNode = tvTOC.Nodes.Add(descNodes[iItem].InnerText);
switch(levelNodes[iItem].InnerText)
{
case "1":
tnLevelOne = tnMainNode.Nodes.Add(descNodes[iItem].InnerText);
break;
case "2":
tnLevelTwo = tnLevelOne.Nodes.Add(descNodes[iItem].InnerText);
break;
case "3":
tnLevelThree = tnLevelTwo.Nodes.Add(descNodes[iItem].InnerText);
break;
case "4":
tbLevelFour = tnLevelThree.Nodes.Add(descNodes[iItem].InnerText);
break;
}
}

I created the custom node inherited from TreeNode and add my own data and
replace the TreeNode objects above with it to be

TreeNodeEx tnMainNode = null;
TreeNodeEx tnLevelOne = null;
TreeNodeEx tnLevelTwo = null;
TreeNodeEx tnLevelThree = null;
TreeNodeEx tbLevelFour = null;

but when add it return TreeNode when ..so cast it to TreeNodeEx...
exception said Specified cast is not valid...what's wrong?

Carlos J. Quintero said:
You can create a TreeNodeEx class which inherits from TreeNode and which
contains additional properties and then you add instances of it to the
treeview. When a TreeNode is clicked, you cast back to TreeNodeEx and
retrieve the additional data.
 

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