tags:

views:

272

answers:

5

I'm busy with a simple application. It reads xml and puts the information in a treeview.

I do this by creating TreeNodes and nest them, and finaly, return the root treenode. Because I want to show some extra information when a treenode is selected, I put the information in the tag property of the TreeNode. In this way, I should be able to retrieve the information when the node is selected.

But when I try to retrieve the information in the Tag property, it says the value = null.

Here is the code where I fill the tag. This is in a function which is recursively used to read the XML dom. treeNode is a paramater given to this function.

if (treeNode.Tag == null)
{
treeNode.Tag = new List<AttributePair>();
}
(treeNode.Tag as List<AttributePair>).Add(new AttributePair(currentNode.Name, currentNode.Value));

This is the event where a treenode is selected

private void tvXML_AfterSelect(object sender, TreeViewEventArgs e)
{
    if (tvXML.SelectedNode.Tag != null)
    {
    }
    if (e.Node.Tag != null)
    {
    }
}

Both values evaluate to null. How can I solve this problem?

A: 

Maybe you are assigning the values after Select event. Otherwise you can maintain a dictionary of TreeNode and tag values as workaround.

Ashok Ralhan
the class that assigns the value, builds the treenodes, so it is not possible that it assigns it after the select event. I thought of that workaround too, but i would like to avoid it
Ikke
+3  A: 

If using Tag property isn't in principle, I'm recommend inherit TreeItem:

public class MyTreeNode : TreeNode
{
    public List<AttributePair> list; 

    public MyTreeNode (string text,List<AttributePair> list) : base(text) 
    {
       this.list = list;
    }
    //or
    public MyTreeNode (string text) : base(text) 
    {
       this.list = new List<AttributePair>();
    }
}

And use it:

private void tvXML_AfterSelect(object sender, TreeViewEventArgs e)
{
    if (tvXML.SelectedNode is MyTreeNode)
    {
       MyTreeNode selectedNode = tvXML.SelectedNode as MyTreeNode; 
       selectedNode.list.Add(.., ..);
    }
    if (e.Node is MyTreeNode)
    {
       MyTreeNode node = e.Node as MyTreeNode; 
       node.list.Add(.., ..);
    }
}
Chernikov
+8  A: 

The code you posted should work as-is. Something else in your code, code that you didn't post here, is causing this to break. It could be clearing the Tag, it could be a data binding set on the tag, etc.

Without seeing all your code, the best I can do is guess and help you isolate the problem.

Here's what I'd do: setup Visual Studio to allow stepping into the .NET framework source code with the debugger. Then, set a breakpoint on the setter for the TreeNode.Tag property. After you set the tag in your code to your AttributePair List, see when it gets set again. The breakpoint will hit, you'll look at the stack trace and see what exactly is clearing your Tag property.

Judah Himango
This seems like a very viable answer...
JustLoren
A: 

Try declaring/initialising your List object somewhere above (outside of the inner scope you are in) and when you assign to the .tag property - don't create a new list but rather assign previously created List object.

DmitryK
A: 

Without seeing the rest of your code, we have to make educated guesses. Given what you said in the comments to your question

In between, i do not touch the tag property anymore. And yes, i've used the debugger. In the first method, i see the tag getting it's value, and in the seccond, i see the value is empty.

I would say the control generating the event (the one received through the parameter sender) is not tvXML.

Do you have other TreeViews on this form? Any chance you accidentally bound the event to the wrong instance of TreeView?

An easy way to check this is comparing sender with tvXML.


private void tvXML_AfterSelect(object sender, TreeViewEventArgs e) {
    // FOR DEBUG: Check it is the right instance
    if (sender != tvXML) {
        throw new InvalidOperationException();
    }

    if (tvXML.SelectedNode.Tag != null) {
    }
    if (e.Node.Tag != null) {
    }
}

Alfred Myers
I only have one Treeview object in my project. I tried to use sender in stead of tvXML, but that makes no difference.
Ikke