ControlsEx Treeview get parent node?

RB Smissaert

Well-Known Member
Licensed User
Longtime User
Is there any simple way to get the parent of a given node in the Treeview?
Also it looks that child nodes always have the index -1. This is when I do:
Tree.IndexOfNode(Tree.SelectedNode) So, I can't get the parent of a child with a loop as I can't get the index of the child node.

RBS
 

specci48

Well-Known Member
Licensed User
Longtime User
Hello RB Smissaert,

the index -1 indicates that currently no node is selected. You can proof this situation with the IsNull statement. E.g.:
B4X:
If IsNull(Tree.SelectedNode) Then
    ' do nothing
Else
    ' do something
End If

To get the parent node of a child node just use the parent instruction:
B4X:
node2.Value = node1.Parent
As above, this line of code will only run fine if the value of node1 is not null.

And again my personal tip for the treeview ;) :
You might have a look at my ToDoTree application which implements a lot of features around the treeview control.



specci48
 

RB Smissaert

Well-Known Member
Licensed User
Longtime User
> the index -1 indicates that currently no node is selected.

Not with me. I have a node selected, but it still gives me -1.
Will have a look again. Thanks for the tip about the Parent property, didn't
see that in the help and will have a look at that.

RBS
 

specci48

Well-Known Member
Licensed User
Longtime User
Not with me. I have a node selected, but it still gives me -1.

So there is likely a bug left in your implementation... :(

Can you upload some code that shows this behaviour so we can help you finding it?


specci48
 

RB Smissaert

Well-Known Member
Licensed User
Longtime User
This is the code:

Sub mnuShowTable_Click

'will show the first 20 rows of the selected table
'-------------------------------------------------
Dim lNodeIndex
Dim strTable
Dim strSQL

lNodeIndex = Tree.IndexOfNode(Tree.SelectedNode)

If lNodeIndex = -1 Then
Msgbox("You need to select the node with the table name.", _
"showing table sample", _
cMsgboxOK, _
cMsgboxNone)
Return
End If

strTable = Tree.SelectedText
strSQL = "SELECT * FROM " & strTable & " limit 20"
cmd.CommandText = strSQL
cmd.ExecuteTable("Table1", 20)

Form2.Close

End Sub


RBS
 

RB Smissaert

Well-Known Member
Licensed User
Longtime User
I forgot to post the code that sets up the treeview:

Sub mnuShowDB_Click

Dim i

For i = 0 To Tree.Count - 1
Tree.RemoveNodeAt(0)
Next

Con.BeginTransaction

strSQL = "SELECT name FROM sqlite_master WHERE type = 'table'"

cmd.CommandText = strSQL

Reader.Value = cmd.ExecuteReader

Do While Reader.ReadNextRow = True
Tree.AddNewNode(Reader.GetValue(0))
Loop

Reader.Close

For i = 0 To Tree.Count - 1
Node.Value = Tree.GetNode(i)
cmd.CommandText = "PRAGMA table_info ('" & Node.Text & "')"
Reader.Value = cmd.ExecuteReader
Do While Reader.ReadNextRow = True
Node.AddNewNode(Reader.GetValue(1) & " : " & Reader.GetValue(2))
Loop
Reader.Close
Next i

Con.EndTransaction

Form2.Show

End Sub


RBS
 

specci48

Well-Known Member
Licensed User
Longtime User
Hi RB Smissaert,

instead of
B4X:
For i = 0 To Tree.Count - 1
    Tree.RemoveNodeAt(0)
Next
you can code
B4X:
Tree.RemoveAllNodes

And you can replace
B4X:
Dim lNodeIndex

lNodeIndex = Tree.IndexOfNode(Tree.SelectedNode)

If lNodeIndex = -1 Then
    Msgbox("You need to select the node with the table name.", _"showingtable sample", _cMsgboxOK, _cMsgboxNone)
    Return
End If
with
B4X:
If IsNull(Tree.SelectedNode) Then
    Msgbox("You need to select the node with the table name.", _"showingtable sample", _cMsgboxOK, _cMsgboxNone)
    Return
End If[


specci48
 

RB Smissaert

Well-Known Member
Licensed User
Longtime User
Thanks for the tips, but does that solve my problem, which is that I get a node index of -1 even when a node is selected?

RBS
 

specci48

Well-Known Member
Licensed User
Longtime User
Where did you get your node index = -1 ?

According to the help file IndexOfNode returns -1 if the node does not exist.
 

Cableguy

Expert
Licensed User
Longtime User
I believe that for each "level" of childs, a new node name must be given, by adding it to the objects references...
something like:

root
!-Level1
!---|-level2
!-Level1
!-Level1
----!Level2
 
Top