Sub Process_Globals
Private fx As JFX
Private MainForm As Form
Private TreeView1 As TreeView
End Sub
Sub AppStart (Form1 As Form, Args() As String)
MainForm = Form1
MainForm.Show
MainForm.RootPane.LoadLayout("1")
FillTree
Dim res As TreeItem = Search(TreeView1.Root, "23")
If res.IsInitialized Then
Dim i As Int = res.Parent.Children.IndexOf(res)
res.Parent.Children.RemoveAt(i)
End If
End Sub
'Returns an uninitialized TreeItem if not found.
Sub Search(Parent As TreeItem, s As String) As TreeItem
For Each ti As TreeItem In Parent.Children
If...
Sub Process_Globals
Private fx As JFX
Private MainForm As Form
Private TreeView1 As TreeView
End Sub
Sub AppStart (Form1 As Form, Args() As String)
MainForm = Form1
MainForm.Show
MainForm.RootPane.LoadLayout("1")
FillTree
Dim res As TreeItem = Search(TreeView1.Root, "23")
If res.IsInitialized Then
Dim i As Int = res.Parent.Children.IndexOf(res)
res.Parent.Children.RemoveAt(i)
End If
End Sub
'Returns an uninitialized TreeItem if not found.
Sub Search(Parent As TreeItem, s As String) As TreeItem
For Each ti As TreeItem In Parent.Children
If ti.Text = s Then
Return ti
End If
If ti.Children.Size > 0 Then
Dim res As TreeItem = Search(ti, s)
If res.IsInitialized Then Return res
End If
Next
Dim res As TreeItem
Return res
End Sub
Sub FillTree
Dim n As TreeItem = TreeView1.Root
For i = 1 To 100
Dim ti As TreeItem = CreateNode(i)
n.Children.Add(ti)
n.Expanded = True
Dim r As Int = Rnd(0, 4)
If r = 1 Then
n = ti
Else if r = 0 And n.Root = False Then
n = n.Parent
End If
Next
End Sub
Sub CreateNode(s As String) As TreeItem
Dim ti As TreeItem
ti.Initialize("", s)
Return ti
End Sub
Thanks a lot Erel, this will do. Perfect!!!The following code creates a random tree and then removes a tree item based on the text.
Create a layout file with a TreeView named TreeView1.
B4X:Sub Process_Globals Private fx As JFX Private MainForm As Form Private TreeView1 As TreeView End Sub Sub AppStart (Form1 As Form, Args() As String) MainForm = Form1 MainForm.Show MainForm.RootPane.LoadLayout("1") FillTree Dim res As TreeItem = Search(TreeView1.Root, "23") If res.IsInitialized Then Dim i As Int = res.Parent.Children.IndexOf(res) res.Parent.Children.RemoveAt(i) End If End Sub 'Returns an uninitialized TreeItem if not found. Sub Search(Parent As TreeItem, s As String) As TreeItem For Each ti As TreeItem In Parent.Children If ti.Text = s Then Return ti End If If ti.Children.Size > 0 Then Dim res As TreeItem = Search(ti, s) If res.IsInitialized Then Return res End If Next Dim res As TreeItem Return res End Sub Sub FillTree Dim n As TreeItem = TreeView1.Root For i = 1 To 100 Dim ti As TreeItem = CreateNode(i) n.Children.Add(ti) n.Expanded = True Dim r As Int = Rnd(0, 4) If r = 1 Then n = ti Else if r = 0 And n.Root = False Then n = n.Parent End If Next End Sub Sub CreateNode(s As String) As TreeItem Dim ti As TreeItem ti.Initialize("", s) Return ti End Sub