• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1889
  • Last Modified:

TreeView To/From text file

i have a project that uses a treeview and text file so the treeview's nodes and sub-nodes will be loaded from this text file and when clicking ( the event:onchange of each node its data will be read into the TEdit fields ) so :

01- how could i load the treeview nodes and sub-nodes from a text file .

02- how could i read each node data into the TEdit fields ( the event on change of each node ).

03-i want give each node an ID so that i can update this text file then this treeview will be automatically updated because its nodes are from this text file so i think working with IDs is good idea.

1 Solution
In which language you want that solution...?

You need text file, why not you use xml file..? this is also a text file and easy to manupulate robust technique
christimageAuthor Commented:
In C++ Well i was thinking text file would be the easiest solution forme since I'm not yet very comon with visual C++although i have done some work with php and XMl before.
christimageAuthor Commented:
I'm using C++ Builder for this project.
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

You say TEdit field so I am assuming you are either working with Delphi or C++ Builder
the code samples here are C++ Builder but should be similar for Delphi.

Firstly you will need some way of identifying the root node/top level nodes and the sub nodes
and how they are structured XML would be ideal for this

if you are using Delphi or C++ Builder you might want to look at http://jvcl.sourceforge.net/
the JvAppXMLFileStorage componet is very handy for the processing of xml files

To look something roughly like this.
<level_1  = &#8220;first node&#8221; >
  <level_2 = &#8220;first sub node&#8221;>
<level_1 = &#8220;second node&#8221; >
  <level_2 = &#8220;etc&#8221;>

you would load this into the tree view.

Alternately you could use the SaveTofile function call to save a current tree view and LoadFromFile function to load it again. This would be a bit limiting as it is a tab
delimited text file you are working with here, and not easily transportable.

//using TTreeView
  TTreeView *TreeView1;
//only for the first node to be added
     TreeView1->Items->Add(NULL,&#8221;first node&#8221;);
// to add subsequent  items to the tree view at level one
      TreeView1->Items->Add(TreeView1->Items->Item[0], AnsiString_variable_here);

//to add sub nodes  you would have to get the node you want to add data to
  TTreeNode *Node1;
  Node1 = TreeView1->Items->Item[nodecount];

//then add the sub node item to the given node in the tree
    TreeView1->Items->AddChild(Node1, AnsiString_variable_here);
Note, for navigating the tree, the following are useful for finding your way around
Node1->ItemId  ( you might be able to use this for your node an ID idea)

To read each node data into a TEdit field depends on what you are doing and
 how you present it to the user.

If you are only going to allow one node in the tree to be selected at a time it
is relatively strait forward.
  TTreeNode *Node1;
  Node1 = TreeView1->Selected;
  Edit1->Text = Node1->Text;

Mulit selects are a bit more tricky.
hope this helps

christimageAuthor Commented:
Does anybody has a demo of this adding tree from text file and saving back to text file. C++
Mike TomlinsonMiddle School Assistant TeacherCommented:
Check out my second example here on how to build a TreeView structure based on input:

It's in C# but I think you'll be able to understand the algorithm...
this makes a treeview with data and saves to text file
  TTreeNode *Node1;
  int nodecount;

  TreeView1->Items->Clear();// remove any existing nodes
  nodecount = TreeView1->Items->Count - 1;
  Node1 = TreeView1->Items->Item[nodecount];
  TreeView1->Items->AddChild(Node1,"one of one");
  nodecount = TreeView1->Items->Count - 1;
  Node1 = TreeView1->Items->Item[nodecount];
  TreeView1->Items->AddChild(Node1,"two of two");
  nodecount = TreeView1->Items->Count - 1;
  Node1 = TreeView1->Items->Item[nodecount];
  TreeView1->Items->AddChild(Node1,"three of three");
  nodecount = TreeView1->Items->Count - 1;
  Node1 = TreeView1->Items->Item[nodecount];
  TreeView1->Items->AddChild(Node1,"four of four");
  nodecount = TreeView1->Items->Count - 1;
  Node1 = TreeView1->Items->Item[nodecount];
  TreeView1->Items->AddChild(Node1,"five of five");

this loads the saved data from the text file

  TreeView1->Items->Clear();// remove any existing nodes
christimageAuthor Commented:
Hi pmdw thanks for your comments I like your first with the XML file but although I'm not yet very clear of how to add the xml root and subitems. The second post the saving of the tree i TreeView1->SaveToFile("c:\\temp\\test.txt"); working I tried something like that before just for testing savetofile function but It does'nt save treeview items.
Personally I find having the JEDI Libraries makes XML processing a whole lot easier.
If you want try that go to install a copy of the JEDI Libraries you then want either a
JvSimpleXML or JvAppXMLFileStorage component on the form you are working on.

And then the following in your code.
( I have hacked this out of one of my programs so it is not perfect but is functional)

//start main function
  int nodecount;
  int nodelevel;
  TJvSimpleXML *XmlFile;
  // normaly you set this from component or user screen input etc..
  AnsiString FileName = "c:\\temp\\test.xml";

  XmlFile = JvAppXMLFileStorage1->Xml;
  XmlFile->LoadFromFile(FileName); //
  ParseIntoTreeView(XmlFile->Root, TreeView1->Items->Add(NULL,ExtractFileName(FileName)));
//end  main function

void TForm1::ParseIntoTreeView(TJvSimpleXMLElem* AnXMLNode, TTreeNode* ATreeNode)
  if (AnXMLNode != NULL)
    AnsiString S;
    if (AnXMLNode->Value != "")
      S = AnXMLNode->Name + "=" + AnXMLNode->Value;
      S = AnXMLNode->Name;
    AnsiString T = "";
    for (int j = 0; j < AnXMLNode->Properties->Count; j++)
      T = T + " " + AnXMLNode->Properties->Item[j]->Name + "=\"" + AnXMLNode->Properties->Item[j]->Value + "\"";
    ATreeNode = TreeView1->Items->AddChild(ATreeNode, S + " (" + Trim(T) + ")");
    for (int i = 0; i < AnXMLNode->Items->Count; i++)
      ParseIntoTreeView(AnXMLNode->Items->Item[i], ATreeNode);

Please note. I think I originally found some of this in the examples that you get with the
JEDI Libraries so you might get a better or fuller example there :-)

> The second post the saving of the tree i TreeView1->SaveToFile("c:\\temp\\test.txt");
> working I tried something like that before just for testing savetofile function but
> It does'nt save treeview items.

Not quite sure if I understand you correctly here, are you saying..
(a)That this does not save the items (tree nodes) in the tree view ?
or are you saying
(b) You have items(meta data) associated with the tree nodes in the tree view that
are not being saved?

If  (a) then there is something else wrong as this is what is supposed to happen.

if (b) then you will have to use an XML file and independently save each save each tree node item together with the associated meta data.


Featured Post


Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now