HOWTO: Saving the Viewing Order of Nodes in a TreeViewLast reviewed: February 10, 1997Article ID: Q161735 |
The information in this article applies to:
SUMMARYThe sample BldTree.scx, located in the Vfp\Samples\Solution\Ole folder, is an example of how to use the TreeView Control. It allows you to add nodes to the TreeView and then save and restore the Nodes from a table. The algorithm that the sample uses saves all of the Nodes by index order. Because of this, it does not restore the Nodes in the same order if you added nodes using "Previous" for the Relationship. This article shows an algorithm that lets you save the Nodes in viewing order so that they can be restored in the same order.
MORE INFORMATIONThis article uses the BldTree.scx as a starting point. It will have you add two new command buttons to the form: one to add Nodes to the TreeView Control and a second to save the Nodes in viewing order to a table. The BldTree.scx can be found in the Vfp\Samples\Solution\Ole folder. For this article, set the above folder as the default by entering the following command in the Command window.
SET DEFAULT TO SYS(2004)+'SAMPLES\SOLUTION\OLE'
o = THISFORM.oleTree
IF !ISNULL(o.SelectedItem)
o.Nodes.Add(o.SelectedItem.Key, 3, THISFORM.NewKey(), "previous",0)
ENDIF
LOCAL loNodes, lcParent, lcDBFName, lcOldAlias, lcOldSafety, liIndex
LOCAL liTmp
#DEFINE WARNING_LOC "Continuing will destroy all data in the " + ;
"table and create a new table with three fields." + CHR(13) + ;
"Do you want to continue?"
#DEFINE WARN_LOC "Warning"
lcOldAlias = ALIAS()
lcOldSafety = SET("SAFETY")
lcDBFName = GETFILE("dbf")
IF EMPTY(lcDBFName) && User chose Cancel
RETURN
ENDIF
IF FILE(lcDBFName)
IF THISFORM.OpenDBF(lcDBFName, .T.) AND ;
THISFORM.VerifyTableStructure() AND ;
MESSAGEBOX(WARNING_LOC,48+256+4,WARN_LOC) = 6
SET SAFETY OFF
ZAP
SET SAFETY &lcOldSafety
ELSE
RETURN
ENDIF
ELSE
CREATE TABLE (lcDBFName) ;
(Key c(4), Parent c(4), Text c(60))
ENDIF
loNodes = THISFORM.oleTree.Nodes
FOR i = 1 to loNodes.Count
IF ISNULL(loNodes.Item(i).Parent)
liIndex = loNodes.Item(i).FirstSibling.Index
liTmp = liIndex
EXIT
ENDIF
NEXT
lcParent = "0_" &&Root
INSERT INTO (lcDBFName) VALUES ;
(loNodes.Item(liIndex).key, ;
lcParent, ;
loNodes.Item(liIndex).Text)
DO WHILE liIndex <> loNodes.Item(liTmp).LastSibling.Index
INSERT INTO (lcDBFName) VALUES ;
(loNodes.Item(liIndex).Next.key, ;
lcParent, ;
loNodes.Item(liIndex).Next.Text)
liIndex = loNodes.Item(liIndex).Next.Index
ENDDO
FOR i = 1 TO loNodes.Count
IF loNodes.Item(i).Children > 0
liIndex = loNodes.Item(i).Child.Index
liTmp = liIndex
INSERT INTO (lcDBFName) VALUES ;
(loNodes.Item(liIndex).key, ;
loNodes.Item(liIndex).Parent.Key, ;
loNodes.Item(liIndex).Text)
DO WHILE liIndex <> loNodes.Item(liTmp).LastSibling.Index
INSERT INTO (lcDBFName) VALUES ;
(loNodes.Item(liIndex).Next.key, ;
loNodes.Item(liIndex).Next.Parent.Key, ;
loNodes.Item(liIndex).Next.Text)
liIndex = loNodes.Item(liIndex).Next.Index
ENDDO
ENDIF
NEXT
USE
You will see that the OldSave.dbf loaded the Node added with the Add Previous button to the bottom of the TreeView Control. But the NewSave.dbf had the Node added with the Add Previous button in the correct position. (c) Microsoft Corporation 1997, All Rights Reserved. Contributions by Brian Combs, Microsoft Corporation |
KBCategory: kbother kbhowto
© 1998 Microsoft Corporation. All rights reserved. Terms of Use. |