Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Remembering TreeView Selections?

Status
Not open for further replies.

stanlyn

Programmer
Sep 3, 2003
945
US
Hi,

I need some pointers and example code on how to save and restore selections the user makes in a windows explorer like treeview control.

Also, any recommendations on a good windows explorer control that allows multiple nodes to be selected?

Lets say MyJob1 which is a vfp record in the jobs table and the user selects multiple nodes in the treeview control that represents files and/or folders. I need a way to save/remember that selection to the vfp job record and repopulate the treeview when navigating back to it.

Thanks, Stanley
 
Stanley,

It's not clear whether you are talking about the standard Microsoft Treeview Control (the one that comes with VFP) or something else.

Assuming you are referring to the standard control, then saving and restoring the selection is easy.

In the Nodeclick event, write code similar to the following:

Code:
LPARAMETER node
lnIndex = node.Index
* Write code here to save lnIndex in a table, text file, registry key
* or any other convenient place.

When you're ready to restore the selected node:

Code:
* Write code here to set lnIndex to the value that you saved in the 
* table, text file or whatever
THISFORM.MyTreeview.SelectedItem = THISFORM.MyTreeview.Nodes(lnIndex)

Obviously, it's up to you how you actually store the value of the node index. I assume you have a way of doing that, but if you don't, come back and I or someone else can advise.

Regarding your question about multiple selections, I don't know any way of doing that with the standard treeview. Maybe someone else can answer that.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips, training, consultancy
 
I just had a thought about multiple selections. If you can't find a better solution, use the Checkboxes property to say that you want a checkbox to appear against each node. Let the user tick the checkboxes for the nodes they want to select. Then, in your code, loop through the nodes, looking for those whose Checked property is .T.

Something like this:

Code:
FOR EACH loNode in THISFORM.MyTreeView.nodes
  IF lnNode.Checked
    * This is a selected node
  ENDIF
ENDFOR

Although this doesn't exactly meet your requirements (you asked for a way of selecting multiple nodes rather than checking them), this method is simple to do, and probably easier for the users as well.

Mike



__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips, training, consultancy
 
Mikes answer are fine, but if your data changes and you rebuild the tree, you can't expect nodes to have the same node indexes, so all I would change is storing the record IDs instead of node ids, and when rebuilding the tree, you can mark the nodes checked or selected, which correspond to record IDs. In this sense it's totally equal to storing selections selected in a multiselect listbox or any other control, the tree structure doesn't change anything about that principle, which even is true on the database level without any controls: To reference a record, store it's primary key, it's there for referencing and referencing the a record.

If the records can come from different tables then of course you need to store both the source table and ID to reidentify it. But that's all, there is no further magic needed to do that for treeviews than for any other multiselection control.

As for what treeview to use, the Microsoft treeview often doesn't fit all needs, I experienced it to be sluggish and "stubborn" when you fill in more than just a few nodes. VFP developers often use ActiveX control suites by DBI, as they offer VFP sample code. Another vendor giving foxpro sample code with its controls is Exontrol. Both DBI and Exontrol offer their controls as a trial version with restricted functionality, but enough to test what fits your needs.

Bye, Olaf.
 
One issue with DBI is that you have to buy their entire suite - you can't just get the treeview on its own (at least, that was the case the last time I looked, which was a while ago).

Another third-party control to consider is Codejock's Report Grid. Essentially, it's a grid that you can populate in a hierarchical, tree-like fashion. I've used it a lot. It's functionality is excellent, but the documentation and support is well below average, and they make no concessions to VFP.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips, training, consultancy
 
Thanks Mike and Olaf,

The control I need needs to have checkboxes next to their corresponding node as Mike offered so multi-selecting is possible. Because I will be using it to select both files and folders as part of a single job, it must be fast and be able to support more than 32,000 nodes. Some of the selected nodes will be files and if a node that represents a folder is checked, then all file nodes below it must be checked as well. Many of our folders contains 800-900 files and there are hundreds of such folders.

So, how would we indicate a folder node that only had partial files selected, as selecting the folder node would represent all sub nodes, and not the partial list.

Olaf, you speak of saving the record number instead of its node id. My question is "record number to what"? If its a table record number, then a pack would mess the automated reselection, just as the node id would be messed up if a new file or folder is added or deleted since the last save. (correct?)

Picture this...
In my ftp application, MyJob1 will use a treeview so the user can select files and folders that needs to be ftp'ed. This selection needs to be available for all drives on the machine. Also note that MyJob1 is only a single record in the Jobs table. MyJob2 can containg an entirely different selection. Also new files and folders will be added and deleted at any time and the needed control must give us a means to deal with these changes.

I'm off to look at some of the mentioned controls...

Thanks, Stanley
 
No I didn't wrote about record numbers, where? Can you quote?

But I only now got aware, that you don't just want the looks of a file explorer, your treeview should really show folders and files. Well, then you need to store the full names as there is no other key to remember for each file or folder. You always need to save key values, not index numbers or record numbers, only key values are the key to reidentify a record or file or folder and there is no other key to a file or folder, but it's full name.

Also note that MyJob1 is only a single record in the Jobs table

Well, you can have a head record for an FTP job, but you will need a hierarchy of sub tables, at least one, to store the selection of folders and files. There is no point in cramping everything into one record. Keep your database tables normalised, that is multiple items normally have multiple records and so you need multiple tables for an FTP job, one for the job header, eg a job name, destination FTP server, login, password, etc. and another one for the list of files and folders.

Bye, Olaf.
 
Stanlyn,

Do you remember thread184-1682504?
You already had a recommendation for a file explorer ActiveX there fr4om clipper01, which you were testing and happy with.

Bye, Olaf.
 
Stanley,

To answer some of your points:

it must be fast and be able to support more than 32,000 nodes

The Microsoft Treeview control will struggle with that number. To be exact, it will be slow in populating the nodes. But once that's done, all the navigating and drilling should be fast.

It's possible to improve matters by populating the tree "on demand". By that I mean that you start by populating the top-level nodes. Then, when the user drills into a node for the first time, you populate that node's lower-level nodes. And so on. That will make the whole thing faster, but at the expense of more complex programming.

So, how would we indicate a folder node that only had partial files selected, as selecting the folder node would represent all sub nodes, and not the partial list.

Checkboxes in Windows have not two but three possible settings. As well as being checked and cleared, a checkbox can contain a "mixed" setting, which looks like checkmark with a grey background. This is what you need in order to show a mixture of selected and unselected nodes.

In VFP, you establish this third state by giving the checkbox an integer value other than 0 or 1. I'm not sure how to do it with the Treeview control, but I assume it's possible.

... you speak of saving the record number instead of its node id. My question is "record number to what"?

I know what you mean. The point is that whatever value you save, it should be something that doesn't change as the tree is refreshed. It would normally be a record ID rather than a record number. In the case of files and folders, I don't know what you would store, but that's an application problem, not something that's fundamental to the use of a treeview.

I'm off to look at some of the mentioned controls...

Good idea. But keep in mind that the treeview is only half of the control. You also need a way of showing individual files. I suggested using the Microsoft Listview control for that, as this exactly mimics the right-hand pane in Windows Explorer.

In fact, using the two Microsoft controls (Listview and Treeview) side by side will give you the most realistic representation of Windows Explorer, as you will be able to closely mimic its look and feel.

Mike






__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips, training, consultancy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top