Example program - A file/directory browser using a tree control
The following example program uses a tree control
DEFFORM Form1()={.Form,.Form$,.Style=0x50C000C4,.Width=316,.Height=212,.Text$="Tree demo",.Id=1024},\
{.ok,.button$,.Style=0x50010001,.Left=261,.Top=5,.Width=50,.Height=14,.Text$="OK",.__Anchor=5,.Id=1},\
{.cancel,.button$,.Style=0x50010000,.Left=261,.Top=22,.Width=50,.Height=14,.Text$="Cancel",.__Anchor=5,.Id=2},\
{.help,.button$,.Style=0x50010000,.Left=260,.Top=42,.Width=50,.Height=14,.Text$="&Collapse",.__Anchor=5,.Id=9},\
{.treeControl1,.tree$,.Style=0x50A10027,.Left=18,.Top=14,.Width=188,.Height=177,.Id=1000,.HasPictures=1,.Sorted=1},\
{.menu1,.Menu$,.Style=0x01,.Id=1001,.option1={.Text$="Option1"},.separator={.Flag=2048},.option2={.Flag=128,.Text$="Option2"}},\
{.txtControl1,.static$,.Style=0x50000000,.Left=211,.Top=68,.Width=95,.Height=13,.Id=1002},\
{.txtControl2,.static$,.Style=0x50000000,.Left=211,.Top=88,.Width=96,.Height=13,.Id=1003},\
{.txtControl3,.static$,.Style=0x50000000,.Left=211,.Top=108,.Width=99,.Height=12,.Id=1004}
LOCAL DIM a(4), FileSystem
LOCAL DIM count, i, root$16
LOCAL DIM pathlen=64
DEFEVENT Form1.Enter()
REM fill tree with some static elements
a(1) = .treeControl1.Add("Hello1", 1)
a(2) = .treeControl1.Add("PGoodbye2", 0)
a(3) = .treeControl1.Item(a(1)).Add("Hello3", 0)
.treeControl1.Item(a(1)).Expand()
a(4) = .treeControl1.Add("Bonjour4", 0)
REM fill a node with the contents of a filesystem
root$ = (STR($MACHINE,, 1) == "U" ? "/user1" : "c:/")
FileSystem = .treeControl1.Add("File system", TRUE)
END EVENT
DEFEVENT Form1.fileexpand(dir$, parent)
REM subroutine used to populate tree at node 'parent'
REM with directory dir$. This is not an event handler.
LOCAL DIM dir$(1)pathlen, name$pathlen
LOCAL DIM i, count, dir, newidx
REM Note the "B" flag. Same as "N" but just basename
CALL KI_DIR dir$, 1, "TB", pathlen, -SYM(dir$()) TO count
FOR i = 1 TO count
name$ = STR(dir$(i), 2)
IF (STR(dir$(i),, 1) == "D" OR STR(name$,, 1) <> ".")
dir = COND(STR(dir$(i),, 1) == "D")
newidx = .treeControl1.Item(parent).Add(name$, dir + 10)
END IF
NEXT i
END EVENT
DEFEVENT Form1.treeControl1.Expand()
LOCAL DIM name$pathlen, dir$pathlen, tdir$pathlen,a,p
a = ..ExpandItem
IF (a >= FileSystem)
IF (a == FileSystem)
dir$ = root$
ELSE
p = a
WHILE TRUE DO
IF (p == FileSystem)
name$ = root$
ELSE
name$ = ..Item(p).Text$
END IF
IF (name$ <> " ")
IF (STR(name$, LEN(name$), 1) <> "/" AND STR(name$, LEN(name$), 1) <> "\")
IF (dir$ <> "")
tdir$ = name$ & "/" & dir$
ELSE
tdir$ = name$
END IF
ELSE
tdir$ = name$ & dir$
END IF
ELSE
tdir$ = "/" & dir$
END IF
dir$ = tdir$
p = ..Item(p).Parent
IF (p <= 0)
BREAK
END IF
WEND
END IF
Form1.fileexpand(dir$, a)
END IF
END EVENT
DEFEVENT Form1.treeControl1.LeftClick()
.txtControl1.Text$ = "Item Index = " & ..Index
.txtControl2.Text$ = "Item Text = " & ..Item(..Index).Text$
END EVENT
DEFEVENT Form1.treeControl1.RightClick()
.menu1.TrackPopup(..Left + ..MouseX + 20, ..Top + ..MouseY)
END EVENT
DEFEVENT Form1.help.Click()
REM collapse tree
Form1.treeControl1.Delete()
Form1.Enter()
END EVENT
DEFEVENT Form1.treeControl1.ExpandChange()
.txtControl3.Text$ = "Parent = " & ..Item(..ExpandItem).Text$
END EVENT
FORM END Form1
Form1.Open()