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

可以在 Windows 窗体 TreeView 控件中创建派生节点或在 ListView 控件中创建派生项。 派生允许添加所需的任何字段,以及用于处理这些字段的自定义方法和构造函数。 此功能的一个用途是将 Customer 对象附加到每个树节点或列表项。 此处的示例适用于控件 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. 可以使用新的派生树节点作为参数来函数调用。

    在下面的示例中,为文本文件的位置设置的路径是“我的文档”文件夹。 这样做是因为可以假定运行 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)、 强制转换和类型转换 (Visual C#),或 强制转换运算符:() (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));
       }
    

另请参阅