none
Extending the treenode in the treeview (C#) RRS feed

  • Question

  • Can someone point me in the direction of a tutorial or example code on
    how to extend the treenode within a treeview.

    I want to be able to store information about each treenode ( over and
    above the text/tag ) that are available as standard.

    thanks
    Richard
    • Moved by OmegaMan Monday, June 1, 2009 4:28 PM (From:Visual C# General)
    Monday, June 1, 2009 10:52 AM

Answers

  • You can create a class or structure to contain a large set of information and then keep the reference to the class/structure in the .Tag. This is what we did for our TreeViews.

    Alternatively, you could create your own TreeView custom control. There are lots of examples I found when googling. Here is one to get your started:

    http://support.microsoft.com/kb/322937

    Hope this helps.
    www.insteptech.com
    We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!
    Monday, June 1, 2009 4:18 PM
  • You can derive your own class from TreeNode and add any members you require.  To get the info back, you have to cast back to your class.  For example:

        public partial class Form1 : Form {
            private class MyNode : TreeNode {
                public DateTime Mumble { get; set; }
                public MyNode(string text, DateTime mumble) : base(text) {
                    Mumble = mumble;
                }
            }
            public Form1() {
                InitializeComponent();
                treeView1.Nodes.Add(new MyNode("Today", DateTime.Now));
            }

            private void treeView1_AfterSelect(object sender, TreeViewEventArgs e) {
                MyNode node = e.Node as MyNode;
                Console.WriteLine(node.Mumble);
            }
        }


    Hans Passant.
    Monday, June 1, 2009 5:20 PM
    Moderator

All replies

  • You can create a class or structure to contain a large set of information and then keep the reference to the class/structure in the .Tag. This is what we did for our TreeViews.

    Alternatively, you could create your own TreeView custom control. There are lots of examples I found when googling. Here is one to get your started:

    http://support.microsoft.com/kb/322937

    Hope this helps.
    www.insteptech.com
    We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!
    Monday, June 1, 2009 4:18 PM
  • You can derive your own class from TreeNode and add any members you require.  To get the info back, you have to cast back to your class.  For example:

        public partial class Form1 : Form {
            private class MyNode : TreeNode {
                public DateTime Mumble { get; set; }
                public MyNode(string text, DateTime mumble) : base(text) {
                    Mumble = mumble;
                }
            }
            public Form1() {
                InitializeComponent();
                treeView1.Nodes.Add(new MyNode("Today", DateTime.Now));
            }

            private void treeView1_AfterSelect(object sender, TreeViewEventArgs e) {
                MyNode node = e.Node as MyNode;
                Console.WriteLine(node.Mumble);
            }
        }


    Hans Passant.
    Monday, June 1, 2009 5:20 PM
    Moderator
  • You can derive your own class from TreeNode and add any members you require.  To get the info back, you have to cast back to your class.  For example:

        public partial class Form1 : Form {
            private class MyNode : TreeNode {
                public DateTime Mumble { get; set; }
                public MyNode(string text, DateTime mumble) : base(text) {
                    Mumble = mumble;
                }
            }
            public Form1() {
                InitializeComponent();
                treeView1.Nodes.Add(new MyNode("Today", DateTime.Now));
            }

            private void treeView1_AfterSelect(object sender, TreeViewEventArgs e) {
                MyNode node = e.Node as MyNode;
                Console.WriteLine(node.Mumble);
            }
        }


    Hans Passant.
    How is this supposed to work? You can't cast a superclass to a subclass. When I try to do it, I get a NullReferenceException.
    Monday, November 28, 2011 10:33 AM