What is a Tree control?
A
Tree
control is a control that displays a hierarchical list
of items. Each item consists of a label, and each
item can have a list of sub-items
associated with it. By clicking an item, the user can
expand and collapse the associated list of sub-items.
<Back>
I noticed that there are two sets of
macro/script actions and control properties for Tree
controls. What are the differences between them?
Yes, there are two set of Tree control
related macro/script actions and control properties.
SprintDB Pro version 2.0 supports
TreeInsertItem(), Tree SetItem(),
TreeDeleteItem(), TreeSortItem() and TreeControl.ItemID(),
TreeControl.RootItemID(), TreeControl.ItemText().
SprintDB Pro
version
2.1 provides
you with an enhanced set of macro actions and control
properties for Tree controls. The name of the actions
and properties end with "Ex".
The
main difference between the earlier version and the
enhanced version is that you can specify the tree item's
ID when you insert tree items by using TreeInsertItemEx()
and thereafter you can use the tree Item's ID when using
TreeSetItemEx(), TreeDeleteItemEx(), TreeSortItemEx().
Note the item ID used in the earlier version of the
macro/script actions is not compatible with the
enhanced actions and properties, therefore, it is
not possible to use the earlier version's actions and
enhanced actions (e.g.: TreeInsertItem() and
TreeSetItemEx() ) together.
We
recommend you to use the enhanced version of the
macro/script actions and control properties if you don't
have macros that are using the old actions and control
properties.
<Back>
How to work with a Tree control?
N ote,
a Tree control cannot be bound to a table field.
This is the difference between a Tree control and other
supported controls like EditBox and ComboBox
controls. Therefore, you need to use the related
macro/script actions and the Tree control's properties
to insert, update or retrieve items in a Tree
control.
<Back>
Tree control Exercise
The following will demonstrate how to work with a Tree
control and its related macro/script actions and the
Tree control's properties. It assumes you have completed
the basic tutorials and are familiar with creating forms
and controls and setting their properties.
Step 1:
Create the tblRegion table and create its fields
Table Name: tbleRegion
|
Field
Name |
Field
Type |
|
ID |
Integer(AutoNumber) |
|
Region |
Text |
|
ParentID |
Integer |
Step 2:
Insert
some basic records in the tblRegion table
|
ID |
Region |
ParentID |
|
1 |
America |
0 |
|
2 |
Europe |
0 |
|
3 |
Canada |
1 |
|
4 |
U.S.A. |
1 |
|
5 |
England |
2 |
|
6 |
France |
2 |
|
7 |
Germany |
2 |
|
8 |
Vancouver |
3 |
|
9 |
Montreal |
3 |
|
10 |
New York |
4 |
|
11 |
San Francisco |
4 |
| ... |
... |
... |

Create a new blank Form
- Tap the Form tab.
- Tap the New button.
- You don't need to choose a RecordSource. Simply
tap the OK button.
Create a Tree control
- Select the Insert > TreeControl menu item.
- Insert the control by tapping in a blank area of
the form and resize the control.
- Select the Tools > Control Properties menu
item, and set the Name property to "tree1".
Add the "InsertItems" button
- This button will retrieve items from tblRegion
table and insert them into the "tree1" control.
- Select the Insert > Button menu item and
then place it on the Form..
- Tap the Tools > Control Properties.
- Set its Name property to "InsertItem".
- Tap the Event tab and in the OnClick
field enter the following lines of code using the
Macro builder.
//Declare local variables that will be used as loop
counters.
Dim(recno1)
Dim(recno2)
Dim(recno3)
//Set the FormSource of SubForm1 to retrieve top level
regions where ParentID is 0.
SetFormSource(SubForm1,
"SELECT tblRegion.* FROM tblRegion WHERE
tblRegion.ParentID=0")
//Set the variable recno1(loop counter for Subform1)
to 1.
SetVar(@recno1,
1)
//Loop through the records in Subform1
//RECCOUNT(1)
returns the total number of records in SubForm1.
DoWhile(@recno1<=RECCOUNT(1))
//Insert item tblRegion.Region with its ID in
tree1.
//!1! refers to the records in Subform1, and !2!
refers to the records in Subform2.
TreeInsertItemEx(&tree1,
, !1!tblRegion.ID, !1!tblRegion.Region)
//Set
the FormSource of SubForm2 to retrieve regions where
ParentID is euqal to the tblRegion.ID of Subform1's
current record.
//This will retrieve all the sub regions of the
current top level region in Subform1.
SetFormSource(SubForm2,
"SELECT tblRegion.* FROM tblRegion WHERE
tblRegion.ParentID=!1!tblRegion.ID")
SetVar(@recno2,
1)
//Loop through the records in Subform2
DoWhile(@recno2<=RECCOUNT(2))
TreeInsertItemEx(&tree1,
!1!tblRegion.ID, !2!tblRegion.ID, !2!tblRegion.Region)
SetFormSource(SubForm3,
"SELECT tblRegion.* FROM tblRegion WHERE
tblRegion.ParentID=!2!tblRegion.ID")
SetVar(@recno3,
1)
//Loop through the records in
Subform3
DoWhile(@recno3<=RECCOUNT(3))
TreeInsertItemEx(&tree1,
!2!tblRegion.ID, !3!tblRegion.ID, !3!tblRegion.Region)
SetVar(@recno3,
@recno3+1)
NextRecord(SubForm3)
EndDoWhile
SetVar(@recno2,
@recno2+1)
NextRecord(SubForm2)
EndDoWhile
//Increase loop counter by 1.
SetVar(@recno1,
@recno1+1)
//Go to the next record.
NextRecord(SubForm1)
EndDoWhile
Test
the Form
-
Tap
File > Run.
-
Tap the
"InsertItems" button, and check that it is working as
you would expect it to.

-
Return to form edit mode
by tapping File > Close.
Add the "Region" EditBox
-
Insert a new EditBox by
tapping the Insert > EditBox menu item and
place it on the Form.
-
Set its Name
property to "Region".
Add the "AddNew" button
-
Insert a new button by
tapping the Insert > Button menu item and place
it on the Form.
-
Tap the Tools >
Control Properties menu item, and set the Caption
to "AddNew".
-
On the Event
tab, enter the following for the OnClick event.
This will insert a new sub region of the currently
selected tree item.
//If
the "Region" edit box is empty this terminates the
execution of the macro immediately by using the
EndMacro() action.
If(&Region=null)
EndMacro
EndIf
//Declare a Local Variable to store the item ID of
the currently selected tree item. This will be the
parent ID of the newly inserted record.
Dim(pid)
//This returns the item ID of the currently
selected item as you omit the arguments of ItemIDEx().
SetVar(@pid,
&tree1.ItemIDEx())
//Add a new record in the "tblRegion" table.
//There
is no need to enter a tblRegion.ID as this will be
generated automatically, using the AutoNumber field
function.
DAddRecord((tblRegion.Region=&Region,tblRegion.ParentID=@pid),
tblRegion)
//Retrieve the tblRegion.ID of the newly created
record and save it to the Local Variable "iid".
Dim(iid)
SetVar(@iid,
DLOOKUP(tblRegion.ID, tblRegion, tblRegion.Region=&Region
AND tblRegion.ParentID=@pid))
//Insert a new tree item in "tree1" as the child
item of the currently selected tree item.
TreeInsertItemEx(&tree1,
@pid, @iid, &Region)
Test
the Form
-
Tap
File > Run.
-
Tap the
"InsertItems" button.
-
Expand
the "Europe", and select "France".
-
Enter
"Paris" in the "Region" edit box.
-
Tap the
"AddNew" button, and check it is working as you would
expect.

-
Return to the form edit
mode by tapping File > Close.
Add
the "DeleteItem" button
-
This will delete the
currently selected tree item.
-
Insert a new button and
set its Caption to "DeleteItem".
-
Enter the following for
the OnClick event.
//Retrieve the currently selected tree item. Note it
will return NULL if there is no selected item in the
tree control.
Dim(iid)
SetVar(@iid,
&tree1.ItemIDEx())
If(@iid<>NULL)
//Delete the record in the "tblRegion" table.
DDeleteRecord(tblRegion,
tblRegion.ID=@iid, No)
//Delete the selected item in the tree control.
TreeDeleteItemEx(&tree1,
@iid)
EndIf
Add
the "UpdateItem" button
-
This will update the
currently selected item text with the text in the
"Region" EditBox.
-
Insert a new button and
set its Caption to "UpdateItem".
-
Enter the following for
the OnClick event.
If(&Region=null)
EndMacro
EndIf
Dim(iid)
SetVar(@iid,
&tree1.ItemIDEx())
//Update the record in the "tblRegion" table with
the new text in "Region" EditBox.
DUpdateRecord(tblRegion.Region=&Region,
tblRegion, tblRegion.ID=@iid, No)
//Update the selected item in the tree control.
TreeSetItemEx(&tree1, @iid, &Region)
Add
the "SortItems" button
-
This will sort the
child items of the currently selected tree item.
-
Insert a new button and
set its Caption to "SortItems".
-
Enter the following for
the OnClick event.
Dim(iid)
SetVar(@iid,&tree1.ItemIDEx())
TreeSortItemEx(&tree1,@iid)
Test
the Form
-
Tap
File > Run, and check the form is working as you
would expect.
-
Return to the form edit
mode by tapping File > Close.
Set
the OnSelChange event of the Tree control
-
This will simply
display the currently selected item and its parent as
well as its next/previous sibling items when the
OnSelChange event occurs. This also demonstrates how
to work with the TreeControl.ItemIDEx() and
TreeControl.ItemTextEx() properties.
The
OnSelChange event occurs whenever the item selection
in the tree control changes.
-
Select the "tree1"
control, and tap Tools > Control Properties.
-
On the Event
tab, enter the following code for the OnSelChange
event.
//Retrieve the currently selected tree item. Note this
will return NULL if there is no selected item in the
tree control.
Dim(iid)
SetVar(@iid,
&tree1.ItemIDEx())
If(@iid=NULL)
EndMacro
EndIf
//Retrieve the next and previous sibling items of the
currently selected tree item.
Dim(nextitem)
Dim(previtem)
SetVar(@nextitem,
IIF(&tree1.ItemIDEx(@iid,"next")=null, "",
&tree1.ItemTextEx(&tree1.ItemIDEx(@iid,"next"))))
SetVar(@previtem,
IIF(&tree1.ItemIDEx(@iid,"previous")=null, "",
&tree1.ItemTextEx(&tree1.ItemIDEx(@iid,"previous"))))
//Retrieve the parent item of the currently selected
tree item.
Dim(pid)
SetVar(@pid,
&tree1.ItemIDEx(@iid, "parent"))
If(@pid=NULL)
MsgBox("Top level
region : " + &tree1.ItemTextEx(@iid) + "\r\nNext
Sibling Item: " + @nextitem + "\r\nPrevious Sibling
Item: " + @previtem,"Selection has changed", OK)
Else
MsgBox("Selected
Item: " + &tree1.ItemTextEx(@iid) + "\r\nParent Item:
" + &tree1.ItemTextEx(@pid) + "\r\nNext Sibling Item:
" + @nextitem + "\r\nPrevious Sibling Item: " + @previtem,
"Selection has changed", OK)
EndIf
-
Tap File > Run
to test the form.

<Back>
|