The code below gives me Node1 Node2 Node3, when I expected Node21 Node22. What is wrong?
In case you know GTK but not GTK# nor C#, here are the signatures of the methods I used. params means variable arguments, out means that the parameter is used as an output, ref` means pass by reference.
- public TreeIter AppendValues(params object[] values)
- public TreeIter AppendValues(TreeIter parent, params object[] values)
- public bool GetIter(out TreeIter iter, TreePath path)
- public bool IterChildren(out TreeIter iter)
- public object GetValue(TreeIter iter, int column)
- public bool IterNext(ref TreeIter iter)
My code
var node1 = treestore.AppendValues(false, "Node1");
var node2 = treestore.AppendValues(false, "Node2");
var node21 = treestore.AppendValues(node2, false, "Node21");
var node22 = treestore.AppendValues(node2, false, "Node22");
var node3 = treestore.AppendValues(false, "Node3");
TreeIter it = new TreeIter();
treestore.GetIter(out it, new TreePath(new[]{1}));
treestore.IterChildren(out it);
do
{
var data = treestore.GetValue(it, 1);
Debug.WriteLine(data);
} while (treestore.IterNext(ref it));
>Solution :
The issue is that you’re starting the iteration at the second item in the tree store and not at node2. You can fix this by starting the iteration at node2 by using node2 as the parent node when calling IterChildren:
var node1 = treestore.AppendValues(false, "Node1");
var node2 = treestore.AppendValues(false, "Node2");
var node21 = treestore.AppendValues(node2, false, "Node21");
var node22 = treestore.AppendValues(node2, false, "Node22");
var node3 = treestore.AppendValues(false, "Node3");
TreeIter it = new TreeIter();
// Start the iteration at node2
treestore.IterChildren(out it, node2);
do
{
var data = treestore.GetValue(it, 1);
Debug.WriteLine(data);
} while (treestore.IterNext(ref it));
This will print Node21 and Node22, as expected. Note that if you want to start the iteration at the first child of node2, you can simply use treestore.GetIterFirst(out it, node2) instead of treestore.IterChildren(out it, node2) in the code above.