TreeView To/From text file

Posted on 2006-05-25
Last Modified: 2007-12-19
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.

Question by:christimage
    LVL 1

    Expert Comment

    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

    Author Comment

    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.

    Author Comment

    I'm using C++ Builder for this project.
    LVL 2

    Expert Comment

    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
    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


    Author Comment

    Does anybody has a demo of this adding tree from text file and saving back to text file. C++
    LVL 85

    Expert Comment

    by:Mike Tomlinson
    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...
    LVL 2

    Accepted Solution

    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

    Author Comment

    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.
    LVL 2

    Expert Comment

    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

    Better Security Awareness With Threat Intelligence

    See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    sum67 challenge 35 76
    pre4 challenge 19 75
    NotAlone Challenge 20 65
    java continue statement 10 52
    Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
    If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
    In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now