Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 294
  • Last Modified:

treeview question

Ok, I've got a treeview containing a list of web pages. They are divided in categories (folders), like sports, arts, etc. A node representing a web page may show something like "Yahoo". However, as you can imagine when you double click on this node I want my program to open IE and go to "http://www.yahoo.com". So, here is where my problem starts, because I don't know how to save this "extra" information. The treeview control let's you manage this extra info with the data property. However, as this property is just a pointer, when I close my program and start it again, all this info is lost. There must be a way (or some control) to save this info to a file. (Note: The treeview control SaveToFile method is not useful here, because it just saves the nodes captions. It doesn't even save the ImageIndex, StateIndex properties of each node).
I believe Lishcke released a control called "VirtualStringList" which included this in the "features" page before download. But, the help file is just INCOMPLETE. So, if anyone there can tell me how to save my own data to a file, so that next time my program opens this file is loaded.

thanks.
0
pin_plunder
Asked:
pin_plunder
1 Solution
 
TheNeilCommented:
Why not simply iterate through the items in the tree and then save their (net) addresses and complete paths out to a text file? When you read them back in you could scan for the appropritae path and assign the (net) address back to the node.

As an algorithm it'd be a case of doing this:

Open file
Output node count
FOR i := 1 TO no items in tree
DO
  Generate path to node (from the root)
  Save path and address to file
Close file

And for reading:

Build tree
Open file
Read node count
FOR i := 1 TO node count
DO
  Read path
  Read address
  Find node on the tree
  Assign the address to the node
Close File

I apologise for the algorithms (rather than code) but as I don't know how you're storing your data then I can't submit any. I also apologise for using node paths BUT (and this is important) node indexes can change (depending on what order they're created in) and I'm assuming that the tree could potentially change (if it doesn't then you can forget about node paths and just iterate straight through the tree)

Hope this helps (even if it's only a tiny bit)

The Neil =:)

 
0
 
lmikleCommented:
Try to use a Data property (type - pointer) of TTreNode to save extra info.

short example

type
 PMyData = ^TMyData;
 TMyData = Record
  URL : String;
  // Other fielsa skiped...
  ...
 end;

...

 var
  Dta : PMyData;
  Node : TTreeNode;

// nodes creating
 for I := 0 to LinkList.items.Count-1 Do
 begin
  // Parse some list, retrive info and create node (skipped)
  // Node  - is a pointer to created node (TTreeNode)
  New(Dta);
  Dta^.URL := LinkList.Items[i]; // Or another var that contain an url
  Node.Data := Dta;
  // Don't dispose Dta here!!!
 end;

// start browser - for example on Dblclick
procedure TreeView1DblClick(...)
var
 URL : String;
begin
 if TreeView1.selected= Nil Then exit;
 if TreeView1.selected.Data=Nil Then exit;
 URL := TMyData(TreeView1.selected.Data^).URL;
 ShellExecute(0,Nil,PChar(URL),Nil,Nil,SW_SHOWNOACTIVATE);
end;
0
 
kretzschmarCommented:
if you use lischke's virtualtree,
then it provides a good streaming ability,
also for the datas

just save it to a stream (filestream) and reload if needed
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
pin_plunderAuthor Commented:
Imikle, I'm sorry but it seems you really didn't understand what happens. Using .Data property is fine, but what happens if you close the program and open it again? Those pointers will point to something else. So, I need to save that "pointed data" to some file so that next time I open my program this file loads.

kretzschmar, it would be great if you give me some code, because I tried some and didn't seem to work.

theneil, although your answer is the most "complicated" that's exactly what I was trying to do. However, you gave me some good advice because I was using absolute indexes to refer to nodes and that was causing problems.

I would prefer kretzschmar to answer first, because I really want to do this with Lischke's control.

thanks all.
0
 
Tom KnowltonWeb developerCommented:
Are you building the TreeView from a data structure?

The best way is to load the information into a data structure, then build the tree from that.  This seperates the GUI from the data.

Did I miss something?
0
 
rondiCommented:
A while back, I succeeded in doing the same thing
with a TListView where each list item had to have
some persisted custom data associated with it.

How ? I created a new ListView control descending from the
VCL's TListView and overrode the component streaming methods that use TReader, TWriter etc.

If you like, I can post some code modified for a
TTreeView, but you've gotta UP the points!!
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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