JavaScript Editor js editor     Web development 

Main Page

File: ...\Samples\Solution\OLE\Bldtree.scx

A treeview control displays a collection of Node objects, each of which consists of a label and an optional bitmap. After creating a treeview control, you can add, remove, arrange, and otherwise manipulate Node objects by setting properties and invoking methods.

This sample illustrates adding nodes to a treeview control, deleting nodes, selecting nodes programmatically, writing a treeview hierarchy to a table, and reading a treeview hierarchy from a table.

Adding Nodes

Each node in a treeview control needs to have a unique key that is a character string, generated by the NewKey method in this sample form.

В Copy Code
*NewKey method
cKey = THIS.cNextKey
THIS.cNextKey = ALLTRIM(STR(VAL(THIS.cNextKey) + 1) + "_")

The Add method of the treeview control is used to add new nodes. The code associated with the Click event of cmdNewNode adds a root level node using the Add method.

В Copy Code
o = THISFORM.oleTree
o.Nodes.Add(,1,THISFORM.NewKey(),"Click to edit text",0)

The code associated with the Click event of cmdNewChild adds a node as a child of the currently selected node.

В Copy Code
o = THISFORM.oleTree
IF !ISNULL(o.SelectedItem) THEN
   o.Nodes.Add(o.SelectedItem.Key, 4, THISFORM.NewKey(), "Click to edit text",0)

Deleting Nodes

You can delete all of the nodes by calling the Clear method.

В Copy Code

Or you can use the Remove method to delete selected nodes. All children of the deleted node are also deleted.

В Copy Code

Writing and Reading Hierarchies in Tables

To save your treeview hierarchy in a table so you can reload it and edit it, loop through all the nodes in the treeview control and write the Key, the parent node's Key, and the Text to the appropriate fields of a table.

В Copy Code
FOR i = 1 TO loNodes.Count
   IF ISNULL(loNodes.Item(i).Parent)
      lcParent = "0_" && Root
      lcParent = loNodes.Item(i).Parent.Key
      (loNodes.Item(i).Key, ;
       lcParent, ;

To reconstruct the treeview hierarchy, scan through the records in the table, using the parent key, key, and text values that you previously stored.

В Copy Code
* Fill the TreeView control with values in the table.
o = THISFORM.oleTree.Nodes
      IF ALLTRIM(parent) = '0_'
         o.add(ALLTRIM(parent),4,ALLTRIM(key), ALLTRIM(text),0)
      THISFORM.cNextKey = ALLTRIM(STR(VAL(key) + 1) + "_")

See Also

JavaScript Editor js editor     Web development