0

I am trying to inherit from TreeNode to add own nodes to TreeView:

class TreeViewItem : TreeNode
{
    public new string Text;

    public override string ToString()
    {
        return "asd";
    }
}

I tried that:

    TreeViewItem tvi = new TreeViewItem();
    tvi.Text = "asd";
    trv_bd_content.Nodes.Add(tvi);

But still getting empty node added to TreeView (node without Text "asd").
What should i do?

Kosmo零
  • 4,001
  • 9
  • 45
  • 88
  • Yes, that won't work. The TreeView class will still use the original Text property, it doesn't know anything about your new Text field. You must use the Text property. – Hans Passant Jul 30 '12 at 15:07
  • Just remove code inside your class and everything will work. Which behaviour are you going to override (modify) in your new class? – George Mamaladze Jul 30 '12 at 15:11
  • i have some object with coords and i want to make when it added to TreeView it were showed like that: public string new Text { get: return name + ": " + latitude + " / " + longitude + " / " + H; } – Kosmo零 Jul 30 '12 at 15:13
  • As I see you want to override Text property which is not virtual. Do `latitude` and 'longitude' change after added to tree or the values are known before you add? – George Mamaladze Jul 30 '12 at 15:18

2 Answers2

2

As I understand you are going to override TreeNode just to display some text you build your own. If values needed for calculation are known before adding the node to tree I would suggest to do it this way:

        treeView = new TreeView();
        var node = new TreeNode(
            string.Format("{0}: {1} / {2} / {3}", name, latitude, longitude, H));
        treeView.Nodes.Add(node);

Overriding a visual element just to keep additional data in it is not a good idea. Usisally you override visual element to modify it's drawing or something related to it's behavior as visual element.

For keeping corresponding data you could use Tag property where you can save any data (untyped). So you can put an instance of a class containing your data.

        node.Tag = "AnyData";
George Mamaladze
  • 7,593
  • 2
  • 36
  • 52
  • This is kinda not what i want. I must have my own **TreeViewItem** added to TreeView, instead of TreeNode. I want easy access to all needed fields inside **TreeViewItem** just by knowing it selected in TreeView. If i store in TreeView TreeNodes, i must search over all instances of another class to find what instance of object math to selected TreeNode in TreeView – Kosmo零 Jul 30 '12 at 15:31
  • Using Tag is good idea! Thanks, i didn't knew about this thing. – Kosmo零 Jul 30 '12 at 15:38
1

It seems to me that the treenode does not use ToString() to get display information. It uses .Text property instead (which doesn't seem to enjoy being overriden )

You could set the Text property in your constructor if you wanted to.

class TreeViewItem : TreeNode
{
    // public new string Text;  Remove this so you don't call it

    public TreeViewItem(){
        this.Text = "asd";
    }

    //Treeview doesn't seem to use this
    public override string ToString()
    {
        return "asd";
    }
}
timothy
  • 568
  • 8
  • 9