Solved

treeview question

Posted on 2001-07-09
6
253 Views
Last Modified: 2010-04-06
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
Comment
Question by:pin_plunder
6 Comments
 
LVL 5

Accepted Solution

by:
TheNeil earned 50 total points
ID: 6265196
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
 
LVL 3

Expert Comment

by:lmikle
ID: 6265307
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
 
LVL 27

Expert Comment

by:kretzschmar
ID: 6265350
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:pin_plunder
ID: 6267018
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
 
LVL 5

Expert Comment

by:knowlton
ID: 6267171
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
 
LVL 3

Expert Comment

by:rondi
ID: 6298573
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…
A company’s greatest vulnerability is their email. CEO fraud, ransomware and spear phishing attacks are the no1 threat to a company’s security. Cybercrime is responsible for the largest loss of money to companies today with losses projected to r…

947 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

22 Experts available now in Live!

Get 1:1 Help Now