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.
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.
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.
ASKER
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 = “first node” >
<level_2 = “first sub node”>
</level_2>
</level_1
<level_1 = “second node” >
<level_2 = “etc”>
</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 ,”fi rst node”);
// to add subsequent items to the tree view at level one
TreeView1->Items->Add(Tree View1->Ite ms->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[nod ecount];
//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
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 = “first node” >
<level_2 = “first sub node”>
</level_2>
</level_1
<level_1 = “second node” >
<level_2 = “etc”>
</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
// to add subsequent items to the tree view at level one
TreeView1->Items->Add(Tree
//to add sub nodes you would have to get the node you want to add data to
TTreeNode *Node1;
Node1 = TreeView1->Items->Item[nod
//then add the sub node item to the given node in the tree
TreeView1->Items->AddChild
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
ASKER
Does anybody has a demo of this adding tree from text file and saving back to text file. C++
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...
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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\\tes t.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->BeginUpd ate();
XmlFile->LoadFromFile(File Name); //
ParseIntoTreeView(XmlFile- >Root, TreeView1->Items->Add(NULL ,ExtractFi leName(Fil eName)));
TreeView1->Items->EndUpdat e();
TreeView1->FullExpand();
//end main function
void TForm1::ParseIntoTreeView( TJvSimpleX MLElem* 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->Cou nt; j++)
T = T + " " + AnXMLNode->Properties->Ite m[j]->Name + "=\"" + AnXMLNode->Properties->Ite m[j]->Valu e + "\"";
ATreeNode = TreeView1->Items->AddChild (ATreeNode , S + " (" + Trim(T) + ")");
for (int i = 0; i < AnXMLNode->Items->Count; i++)
ParseIntoTreeView(AnXMLNod e->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\\tes t.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.
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->BeginUpd
XmlFile->LoadFromFile(File
ParseIntoTreeView(XmlFile-
TreeView1->Items->EndUpdat
TreeView1->FullExpand();
//end main function
void TForm1::ParseIntoTreeView(
{
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->Cou
T = T + " " + AnXMLNode->Properties->Ite
ATreeNode = TreeView1->Items->AddChild
for (int i = 0; i < AnXMLNode->Items->Count; i++)
ParseIntoTreeView(AnXMLNod
}
}
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:\
> 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.
You need text file, why not you use xml file..? this is also a text file and easy to manupulate robust technique