treeview help

  • Thread starter Thread starter MSNEWS
  • Start date Start date
M

MSNEWS

Hi

I'm trying to use the treeview control but having a hard time trying to
visualize how I'm going to achieve when I'm trying to do, as an example in
the end of my program I want my treeview to look something like this :-

ANIMALS
FOUR LEGS
DOGS
POODLE
GREYHOUND
BOXER
PIT BULL
CATS
TWO LEGS
HUMANS
MALE
FEMALE

VEGETABLES
GREEN
APPLIES

I'm trying to write a sub which you will pass to it for example
"ANIMALS/FOUR LEGS" or "ANIMALS/FOUR LEGS/DOGS/POODLE", and this sub will
add a note in the correct place in the node list. I have it working for one
node, but going deeper I can't think how to do it without having duplicate
code for each level (my REAL application will go down a lot of levels)

I'm not expecting anybody to write the sub (unless you want to :-) but I'm
open to some hints!

Thanks
 
Hi,
I have never used treeview, but some kind of recursion should do the
trick, like (pseudo code):

void AddElement(Node currentNode, string stub)
{
int I = stub.IndexOf("/");
string newNodeName = String.Empty;

if (I < 0)
{
//this is last part, so if needed add element
//and escape
if (!ElementExists(currentNode, stub)
currentNode.AddElement(stub);
return;
}

//if we are here, there are more parts in the stub

string newNodeName = stub.SubString(I);

Node nextNode = currentNode.FindNodeByName(newNodeName);

if (nextNode == null)
{
//there is no subnode with that name,
//so we need to create one
nextNode = currentNode.AddNewNode(newNodeName);
}

string nextStub = stub.Substring(I, stub.Length - I);

//recurse
AddElement(nextNode, nextStub);
}


Something like this :)

I Do not know if TreeNode has these methods (AddElement, FindByName,
etc.) or you have to implement them, but still this is the way.

Sunny
 
Back
Top