I use TreeView in my C# project (Windows Forms) and I have a problem (obviously :P). I fill my TreeView like this:
void RefreshTreeView()
{
treeView1.Nodes.Clear();
for (int i = 0 ; i < categories.Count ; ++i)
{
treeView1.Nodes.Add(categories[i].name);
for (int j = 0 ; j < categories[i].questions.Count ; ++j)
{
treeView1.Nodes[i].Nodes.Add("Pytanie nr " + categories[i].questions[j].number.ToString()
+ " za " + categories[i].questions[j].points.ToString() + " pkt. ["
+ (categories[i].questions[j].used ? "Przeczytane" : "Nieprzeczytane") + "]");
treeView1.Nodes[i].Tag = categories[i].questions[j]; // A
}
}
}
In line marked “A” above I attach a Question object to Node’s Tag. And it seems to work as later on in the program I can retrieve my Question object like this:
((Question)treeView1.Nodes[0].Tag).number
But I want to get the currently selected Node’s Tag. Which I try to do like this:
((Question)treeView1.SelectedNode.Tag).number
But it doesn’t work (Tag is null). What am I doing wrong? And how to get the currently selected Node’s Tag?
treeView1.Nodes[i].Tagis the tag of the parent node.You probably want to set the tag of the child node, which is
treeView1.Nodes[i].Node[j].Tag