Link to home
Start Free TrialLog in
Avatar of christimage
christimage

asked on

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.


Avatar of awaiskhan
awaiskhan

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
Avatar of christimage

ASKER

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.
I'm using C++ Builder for this project.
Hi
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.
<tree>
<level_1  = &#8220;first node&#8221; >
  <level_2 = &#8220;first sub node&#8221;>
  </level_2>
</level_1
<level_1 = &#8220;second node&#8221; >
  <level_2 = &#8220;etc&#8221;>
  </level_2>
</level_1
</tree>

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)
Node1->AbsoluteIndex
Node1->Count
Node1->Level

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

Does anybody has a demo of this adding tree from text file and saving back to text file. C++
Avatar of Mike Tomlinson
Check out my second example here on how to build a TreeView structure based on input:
https://www.experts-exchange.com/questions/21823100/populate-treeview.html

It's in C# but I think you'll be able to understand the algorithm...
ASKER CERTIFIED SOLUTION
Avatar of pmdw
pmdw
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
Hi
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;
  TreeView1->Items->Clear();
  TreeView1->Items->BeginUpdate();
  XmlFile->LoadFromFile(FileName); //
  ParseIntoTreeView(XmlFile->Root, TreeView1->Items->Add(NULL,ExtractFileName(FileName)));
  TreeView1->Items->EndUpdate();
  TreeView1->FullExpand();
//end  main function

void TForm1::ParseIntoTreeView(TJvSimpleXMLElem* AnXMLNode, TTreeNode* ATreeNode)
{
  if (AnXMLNode != NULL)
  {
    AnsiString S;
    if (AnXMLNode->Value != "")
      S = AnXMLNode->Name + "=" + AnXMLNode->Value;
    else
      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.