HOWTO: Saving the Viewing Order of Nodes in a TreeView

Last reviewed: February 10, 1997
Article ID: Q161735
The information in this article applies to:
  • Microsoft Visual FoxPro for Windows, version 5.0

SUMMARY

The 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 INFORMATION

This 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'

  • Open the BldTree.scx in the Form Designer.

  • Add two new command buttons to the form. (You may have to change the size of the form to do this.)

  • Change the caption of the first new command button to "Add Previous" (without the quotes).

  • Place the following code in the Click event:

          o = THISFORM.oleTree
          IF !ISNULL(o.SelectedItem)
    
            o.Nodes.Add(o.SelectedItem.Key, 3, THISFORM.NewKey(), "previous",0)
          ENDIF
    
    

  • Change the caption of the second new command button to "Save by View Order" (without the quotes).

  • Place the following code in the Click event:

           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
    
    

  • Save the form as Vfp\Samples\Solution\Ole\Treebld2.scx

  • Run the Form and create a tree by clicking New Root twice. Select the last node in the TreeView control, and then click Add Previous.

  • Click Save DBF to save a table named OldSave.dbf. Then click Save By View Order to save a table named NewSave.dbf.

  • Click Clear, and then click Load DBF to load OldSave.dbf.

  • Repeat step 10 using NewSave.dbf.

    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
    KBSubcategory: FxotherSample vfoxwin
    Additional reference words: 5.00 ActiveX


    THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

    Last reviewed: February 10, 1997
    © 1998 Microsoft Corporation. All rights reserved. Terms of Use.