Adding more then 20 nodes to TREEVIEW all fine here is the simple code:
private void add_Click(object sender, EventArgs e)
{
string st = "node" + cnt.ToString();
TreeNode tn = new TreeNode(st);
tn.Tag = "J" + cnt.ToString();
cnt++;
treeView1.SelectedNode.Nodes.Add(tn);
treeView1.SelectedNode.Expand();
}
BUT if I look at treeView1.HasChildren field its value is == FALSE
there for if I try to get a collection of nodes:
TreeNodeCollection myNodeCollection = treeView1.Nodes;
int myCount = myNodeCollection.Count;
the count is only===> 1 THE ROOT!!!
though I can see the nodes that I have added
and there are more then 20 nodes?????
any clue to whats wrong??
private void add_Click(object sender, EventArgs e)
{
string st = "node" + cnt.ToString();
TreeNode tn = new TreeNode(st);
tn.Tag = "J" + cnt.ToString();
cnt++;
treeView1.SelectedNode.Nodes.Add(tn);
treeView1.SelectedNode.Expand();
}
BUT if I look at treeView1.HasChildren field its value is == FALSE
there for if I try to get a collection of nodes:
TreeNodeCollection myNodeCollection = treeView1.Nodes;
int myCount = myNodeCollection.Count;
the count is only===> 1 THE ROOT!!!
though I can see the nodes that I have added
and there are more then 20 nodes?????
any clue to whats wrong??
SokakPosted Apr 18, 2007, 8:47 PM
To do this, I would recommend using a dynamic-sized container for the data you want to retrieve from the nodes. (Or the nodes themselves) I'll leave it to you to assess what you want to store, this example uses an ArrayList.
In your calling code you would have something like:
ArrayList myNodes = new ArrayList();
findNodes(treeView1.Nodes, myNodes);
// myNodes should now contain all nodes and child, and grandchild (etc) nodes in the treeview.
...
private void findNodes(TreeNodeCollection nodes, ArrayList myNodes)
{
foreach(TreeNode node in nodes)
{
myNodes.Add(node);
if (node.HasChildren)
findNodes(node.Nodes, myNodes);
}
}
TalPosted Apr 18, 2007, 2:44 PM
I need to save the tree in TreeNodeCollection
if (treeView1.HasChildren)
{
myNodeCollection = treeView1.Nodes;
}
int myCount = myNodeCollection.Count;
// Create an Object array.
myArray = new Object[myCount];
// Copy the collection into an array.
myNodeCollection.CopyTo(myArray, 0);
treeView1.Nodes.Clear();
but the problem is that the collection gets only the root node, and If I pass the second node I get only the nodes on that level but not the sons