如何:向 TreeView 或 ListView 控件添加自定义信息(Windows 窗体)

更新:2007 年 11 月

可以在 Windows 窗体 TreeView 控件中创建派生节点,或在 ListView 控件中创建派生项。派生使您得以添加任何所需的字段,和添加处理这些字段的自定义方法和构造函数。此功能的用途之一是将客户对象附加到每个树节点或列表项。虽然这里的示例是关于 TreeView 控件的,但该方法同样适用于 ListView 控件。

派生树节点

  • 创建一个从 TreeNode 类派生的新节点类,此新节点类具有一个记录文件路径的自定义字段。

    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("\\"));
       }
    };
    

使用派生的树节点

  1. 新的派生树节点可用作函数调用的参数。

    在下面的示例中,文本文件位置的路径设置是 My Documents 文件夹。这样做是因为假定大多数运行 Windows 操作系统的计算机都包含此目录。这还将允许具有最低系统访问级别的用户安全地运行应用程序。

    ' 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. 如果传递了这个树节点,且它的类型被声明为 TreeNode 类,则需要将其强制转换为派生类。类型转换是从一种对象类型到另一种对象类型的显式转换。有关强制转换的更多信息,请参见 隐式转换和显式转换 (Visual Basic)、() 运算符(C# 参考) (Visual C#) 或 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));
       }
    

请参见

其他资源

TreeView 控件(Windows 窗体)

ListView 控件(Windows 窗体)