I have a problem with syntax in C#. I need to make my own treeview control in which I just need to add one property to nodes. For an example, if I have list of files listed in my tree, I want to keep filesize somewhere that is related to that node.

Ok. Think about this as you need an application which will list all of your folders in you hard drive as a tree list. And for for every folder, you need to store informations like file size, absolute path etc. On clicking on nodes, you should see this informations for every single node. That's the idea.

Use the TreeNode.Tag property to store information relating to the tree node. Mock up some container class where you can put file and disk use information and set the node's .Tag property to a reference of it.

You can set the ToolTip of the node to these information or you can set the node text = "Folder name - Folder size"

I think, the easiest way is to make a new class MyTreeView which extends TreeView control. Then, I could add as much properties as I want to my nodes, but I have no expieriences in doing that.

You don't need to extend the TreeView control, use the .Tag property and stuff your data in there.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace daniweb
{
  public partial class frmTree : Form
  {
    private bool shown = default(bool);

    public frmTree()
    {
      InitializeComponent();
    }

    private void frmTree_Load(object sender, EventArgs e)
    {
      #region first node
      {
        TreeNode tn = treeView1.Nodes.Add(@"C:\");
        TreeNodeData tnd = new TreeNodeData();
        tnd.Directory = @"C:\";
        tnd.DirectorySize = 12345;
        tn.Tag = tnd;
      }
      #endregion
      #region second node
      {
        TreeNode tn = treeView1.Nodes.Add(@"D:\");
        TreeNodeData tnd = new TreeNodeData();
        tnd.Directory = @"D:\";
        tnd.DirectorySize = 54321;
        tn.Tag = tnd;
      }
      #endregion
    }

    private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
    {
      if (!shown || (treeView1.SelectedNode == null))
        return;
      TreeNodeData tnd = (treeView1.SelectedNode.Tag as TreeNodeData);
      MessageBox.Show(tnd.DirectorySize.ToString("F0"));
    }

    private void frmTree_Shown(object sender, EventArgs e)
    {
      shown = true;
    }
  }
  internal sealed class TreeNodeData
  {
    public string Directory { get; set; }
    public int DirectorySize { get; set; }
    public TreeNodeData() { }
  }
}

Thank for your posts guys. I done it both ways. For those who will read this, just to put an emphasis on some things for better understanding. If you are using tag method, it is important to make a class for your purposes, and then to bind it with tag property:

class MyClass
{
        // your properties and methods
}

MyClass test = new MyClass();
void SomeProcedure()
{
        TreeNode rootNode = new TreeNode();
        rootNode.Text = "name of node";
        rootNode.Tag = test;
        treeView1.Nodes.add(rootNode);
        // your code
}

private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
{
         MyClass data = TreeView1.SelectedNode.Tag as MyClass;
         // data contains what is in selected node tag's
         
         // your code
}

Also, efficient method is to derive your own class from TreeNode and then use it in TreeView control, which will be:

public class MyTreeNode : TreeNode
{
       // your properties
}

void SomeProcedure()
{
        MyTreeNode rootNode = new MyTreeNode();
        rootNode.Text = "name of node";

        treeView1.Nodes.Add(rootNode);
}

private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
{
         MyTreeNode newNode = treeView1.SelectedNode.Tag as MyTreeNode;
         // your code
}

For changing values in properties of nodes, it's good to use some shared public variable, so the other procedures could write in it, and also from which you can read the data in void SomeProcedure() above.

I'm glad to see you have it working!

Please be sure to use code tags in the future when pasting code in to the forum.

This question has already been answered. Start a new discussion instead.