R RB Smissaert Well-Known Member Licensed User Longtime User Dec 16, 2008 #1 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
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 Dec 17, 2008 #2 Hello RB Smissaert, the index -1 indicates that currently no node is selected. You can proof this situation with the IsNull statement. E.g.: Code: 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: Code: 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
Hello RB Smissaert, the index -1 indicates that currently no node is selected. You can proof this situation with the IsNull statement. E.g.: Code: 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: Code: 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
R RB Smissaert Well-Known Member Licensed User Longtime User Dec 17, 2008 #3 > 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
> 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 Dec 17, 2008 #4 RB Smissaert said: Not with me. I have a node selected, but it still gives me -1. Click to expand... 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 said: Not with me. I have a node selected, but it still gives me -1. Click to expand... 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
R RB Smissaert Well-Known Member Licensed User Longtime User Dec 17, 2008 #5 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
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
R RB Smissaert Well-Known Member Licensed User Longtime User Dec 17, 2008 #6 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
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 Dec 18, 2008 #7 Hi RB Smissaert, instead of Code: For i = 0 To Tree.Count - 1 Tree.RemoveNodeAt(0) Next you can code Code: Tree.RemoveAllNodes And you can replace Code: 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 Code: If IsNull(Tree.SelectedNode) Then Msgbox("You need to select the node with the table name.", _"showingtable sample", _cMsgboxOK, _cMsgboxNone) Return End If[ specci48
Hi RB Smissaert, instead of Code: For i = 0 To Tree.Count - 1 Tree.RemoveNodeAt(0) Next you can code Code: Tree.RemoveAllNodes And you can replace Code: 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 Code: If IsNull(Tree.SelectedNode) Then Msgbox("You need to select the node with the table name.", _"showingtable sample", _cMsgboxOK, _cMsgboxNone) Return End If[ specci48
R RB Smissaert Well-Known Member Licensed User Longtime User Dec 18, 2008 #8 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
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 Dec 18, 2008 #9 Where did you get your node index = -1 ? According to the help file IndexOfNode returns -1 if the node does not exist.
Where did you get your node index = -1 ? According to the help file IndexOfNode returns -1 if the node does not exist.
R RB Smissaert Well-Known Member Licensed User Longtime User Dec 18, 2008 #10 When I have a child node selected and run the code I posted first. RBS
Cableguy Expert Licensed User Longtime User Dec 18, 2008 #11 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
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
R RB Smissaert Well-Known Member Licensed User Longtime User Dec 18, 2008 #12 How do you suggest I should change the code? RBS