"类型'System.Windows.Forms.TreeNodeCollection'没有定义构造函数"

Dan*_*npe 4 .net c# collections treeview treenode

我有这个代码:

    private TreeNodeCollection treeCollection;

    public Client(TcpClient c)
    {
        this.tcpClient = c;
        this.tree = null;
        this.treeCollection = new TreeNodeCollection();
        this.treeCollection.Clear();
    }

    public void AddNode(string node)
    {
        this.treeCollection.Add(node);
    }
Run Code Online (Sandbox Code Playgroud)

this.treeCollection = new TreeNodeCollection();回报

The type 'System.Windows.Forms.TreeNodeCollection' has no constructors defined
Run Code Online (Sandbox Code Playgroud)

如果我删除这一行,我得到的treeCollection从未被分配,并且将始终为null ...

Field 'Client.treeCollection' is never assigned to, and will always have its default value null
Run Code Online (Sandbox Code Playgroud)

如何将treeCollection指定为新的TreeNodeCollection,以便我可以使用AddNode方法向其添加节点?

Nei*_*l N 8

TreeNodeCollection有一个内部工厂或构造函数,因此它只能由TreeView控件使用.

但是......你不需要它.只需使用单个节点作为根节点即可.然后清除它的孩子

rootNode.Nodes.Clear();
Run Code Online (Sandbox Code Playgroud)

或者,如果必须,只需创建一个

List<TreeNode>
Run Code Online (Sandbox Code Playgroud)