Partilhar via


How to: Add Custom Information to a TreeView or ListView Control (Windows Forms)

You can create a derived node in a Windows Forms TreeView control or a derived item in a ListView control. Derivation allows you to add any fields you require, as well as custom methods and constructors for handling them. One use of this feature is to attach a Customer object to each tree node or list item. The examples here are for a TreeView control, but the same approach can be used for a ListView control.

To derive a tree node

  • Create a new node class, derived from the TreeNode class, which has a custom field to record a file path.

    Class myTreeNode
       Inherits TreeNode
    
       Public FilePath As String
    
       Sub New(ByVal fp As String)
          MyBase.New()
          FilePath = fp
          Me.Text = fp.Substring(fp.LastIndexOf("\"))
       End Sub
    End Class
    
    class myTreeNode : TreeNode
    {
       public string FilePath;
    
       public myTreeNode(string fp)
       {
          FilePath = fp;
          this.Text = fp.Substring(fp.LastIndexOf("\\"));
       }
    }
    
    ref class myTreeNode : public TreeNode
    {
    public:
       System::String ^ FilePath;
    
       myTreeNode(System::String ^ fp)
       {
          FilePath = fp;
          this->Text = fp->Substring(fp->LastIndexOf("\\"));
       }
    };
    

To use a derived tree node

  1. You can use the new derived tree node as a parameter to function calls.

    In the example below, the path set for the location of the text file is the My Documents folder. This is done because you can assume that most computers running the Windows operating system will include this directory. This also allows users with minimal system access levels to safely run the application.

    ' You should replace the bold text file 
    ' in the sample below with a text file of your own choosing.
    TreeView1.Nodes.Add(New myTreeNode (System.Environment.GetFolderPath _
       (System.Environment.SpecialFolder.Personal) _
       & "\ TextFile.txt ") )
    
    // You should replace the bold text file 
    // in the sample below with a text file of your own choosing.
    // Note the escape character used (@) when specifying the path.
    treeView1.Nodes.Add(new myTreeNode (System.Environment.GetFolderPath _
       (System.Environment.SpecialFolder.Personal) _
       + @"\TextFile.txt") );
    
    // You should replace the bold text file 
    // in the sample below with a text file of your own choosing.
    treeView1->Nodes->Add(new myTreeNode(String::Concat(
       System::Environment::GetFolderPath
       (System::Environment::SpecialFolder::Personal),
       "\\TextFile.txt")));
    
  2. If you are passed the tree node and it is typed as a TreeNode class, then you will need to cast to your derived class. Casting is an explicit conversion from one type of object to another. For more information on casting, see Implicit and Explicit Conversions (Visual Basic), () Operator (C# Reference) (Visual C#), or Cast Operator: () (Visual C++).

    Public Sub TreeView1_AfterSelect(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles TreeView1.AfterSelect
       Dim mynode As myTreeNode
       mynode = CType(e.node, myTreeNode)
       MessageBox.Show("Node selected is " & mynode.filepath)
    End Sub
    
    protected void treeView1_AfterSelect (object sender,
    System.Windows.Forms.TreeViewEventArgs e)
    {
       myTreeNode myNode = (myTreeNode)e.Node;
       MessageBox.Show("Node selected is " + myNode.FilePath);
    }
    
    private:
       System::Void treeView1_AfterSelect(System::Object ^  sender,
          System::Windows::Forms::TreeViewEventArgs ^  e)
       {
          myTreeNode ^ myNode = safe_cast<myTreeNode^>(e->Node);
          MessageBox::Show(String::Concat("Node selected is ", 
             myNode->FilePath));
       }
    

See Also

Other Resources

TreeView Control (Windows Forms)
ListView Control (Windows Forms)