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

时间:2011-07-06 16:45:08

标签: c# .net 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);
    }

this.treeCollection = new TreeNodeCollection();返回

The type 'System.Windows.Forms.TreeNodeCollection' has no constructors defined

如果我删除这一行,我得到的treeCollection永远不会被分配,并且总是为空...

Field 'Client.treeCollection' is never assigned to, and will always have its default value null

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

5 个答案:

答案 0 :(得分:8)

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

但是......你不需要它。只需使用单个节点作为根节点即可。然后用

清除其孩子
rootNode.Nodes.Clear();

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

List<TreeNode>

答案 1 :(得分:2)

似乎TreeNodeCollection不应该由用户创建。相反,它由TreeView类的只读属性“节点”公开。

看到它从.Net 1.0开始就可以使用,我认为它是仿制药不存在时的遗留物,程序员必须通过暴露这些自定义类来强制执行强类型。

今天,更好的设计可能会暴露IList<TreeNode>甚至IDictionary<String, TreeNode>。相反,如果您想自己存储TreeNodes,可以使用List<TreeNode>Dictionary<String, TreeNode>,具体取决于您希望如何访问节点...

Personaly,我更愿意创建System.Collections.ObjectModel.KeyedCollection<String, TreeNode> 的自定义版本(它仍会保持插入顺序中的节点,但也允许键控访问)。您的milleage可能会有所不同。

答案 2 :(得分:1)

您可以通过以下方式获取新的TreeNodeCollection:

public TreeNodeCollection NewTreeNodeCollection(){
    TreeView tmp = new TreeView();
    return tmp.Nodes;
}

答案 3 :(得分:-1)

类UGTreeNodeCollection     {         private TreeNodeCollection NodeCollection;

    public UGTreeNodeCollection(TreeNodeCollection TreeNodeCollectionItem)
    {
        NodeCollection = TreeNodeCollectionItem;
    }

    public TreeNode Add(string text)
    {
        return NodeCollection.Add(text);
    }

    public int Add(TreeNode node)
    {
        return NodeCollection.Add(node);
    }

    public TreeNode Add(string key, string text)
    {
        return NodeCollection.Add(key, text) as TreeNode;
    }

    public TreeNode Add(string key, string text, int imageIndex)
    {
        return NodeCollection.Add(key, text, imageIndex);
    }

    public TreeNode Add(string key, string text, string imageKey)
    {
        return NodeCollection.Add(key, text, imageKey);
    }

    public TreeNode Add(string key, string text, int imageIndex, int selectedImageIndex)
    {
        return NodeCollection.Add(key, text, imageIndex, selectedImageIndex);
    }

    public TreeNode Add(string key, string text, string imageKey, string selectedImageKey)
    {
        return NodeCollection.Add(key, text, imageKey, selectedImageKey);
    }

    public void AddRange(TreeNode[] nodes)
    {
        NodeCollection.AddRange(nodes);
    }

    public ParallelQuery AsParallel()
    {
        return NodeCollection.AsParallel();
    }

    public IQueryable AsQueryable()
    {
        return NodeCollection.AsQueryable();
    }

    public IEnumerable<TResult> Cast<TResult>()
    {
        return NodeCollection.Cast<TResult>();
    }

    public void Clear()
    {
        NodeCollection.Clear();
    }

    public bool Contains(TreeNode node)
    {
        return NodeCollection.Contains(node);
    }

    public bool ContainsKey(string key)
    {
        return NodeCollection.ContainsKey(key);
    }

    public void CopyTo(Array dest, int index)
    {
        NodeCollection.CopyTo(dest, index);
    }

    public int Count
    {
        get
        {
            return NodeCollection.Count;
        }
        private set { }
    }

    public bool Equals(object obj)
    {
        return NodeCollection.Equals(obj);
    }

    public TreeNode[] Finde(string key, bool searchAllChildren)
    {
        return NodeCollection.Find(key, searchAllChildren);
    }

    public IEnumerator GetEnumerator()
    {
        return NodeCollection.GetEnumerator();
    }

    public int GetHashCode()
    {
        return NodeCollection.GetHashCode();
    }

    public Type GetType()
    {
        return NodeCollection.GetType();
    }

    public int IndexOf(TreeNode node)
    {
        return NodeCollection.IndexOf(node);
    }

    public int IndexOfKey(string key)
    {
        return NodeCollection.IndexOfKey(key);
    }

    public TreeNode Insert(int index, string text)
    {
        return NodeCollection.Insert(index, text);
    }

    public void Insert(int index, TreeNode node)
    {
        NodeCollection.Insert(index, node);
    }

    public TreeNode Insert(int index,string key, string text)
    {
        return NodeCollection.Insert(index, key, text);
    }

    public TreeNode Insert(int index, string key, string text,int imageIndex)
    {
        return NodeCollection.Insert(index, key, text, imageIndex);
    }

    public TreeNode Insert(int index, string key, string text, string imageKey)
    {
        return NodeCollection.Insert(index, key, text, imageKey);
    }

    public TreeNode Insert(int index, string key, string text, int imageIndex, int selectedImageIndex)
    {
        return NodeCollection.Insert(index, key, text, imageIndex, selectedImageIndex);
    }

    public TreeNode Insert(int index, string key, string text, string imageKey, string selectedImageKey)
    {
        return NodeCollection.Insert(index, key, text, imageKey, selectedImageKey);
    }

    public bool IsReadyOnly
    {
        get
        {
            return NodeCollection.IsReadOnly;
        }
        private set
        {
        }
    }

    public IEnumerable<TResult> OfType<TResult>()
    {
        return NodeCollection.OfType<TResult>();
    }

    public void Remove(TreeNode node)
    {
        NodeCollection.Remove(node);
    }

    public void RemoveAt(int index)
    {
        NodeCollection.RemoveAt(index);
    }

    public void RemoveByKey(string key)
    {
        NodeCollection.RemoveByKey(key);
    }

    public string ToString()
    {
        return NodeCollection.ToString();
    }
}

答案 4 :(得分:-1)

公共类UGTreeNodeCollection     {

    public UGTreeNodeCollection(TreeNodeCollection TreeNodeCollectionItem)
    {
        NodeCollection = TreeNodeCollectionItem;
    }

    private TreeNodeCollection NodeCollection;
    public TreeNode Add(string text)
    {
        return NodeCollection.Add(text);
    }

    public int Add(TreeNode node)
    {
        return NodeCollection.Add(node);
    }

    public TreeNode Add(string key, string text)
    {
        return NodeCollection.Add(key, text) as TreeNode;
    }

    public TreeNode Add(string key, string text, int imageIndex)
    {
        return NodeCollection.Add(key, text, imageIndex);
    }

    public TreeNode Add(string key, string text, string imageKey)
    {
        return NodeCollection.Add(key, text, imageKey);
    }

    public TreeNode Add(string key, string text, int imageIndex, int selectedImageIndex)
    {
        return NodeCollection.Add(key, text, imageIndex, selectedImageIndex);
    }

    public TreeNode Add(string key, string text, string imageKey, string selectedImageKey)
    {
        return NodeCollection.Add(key, text, imageKey, selectedImageKey);
    }

    public void AddRange(TreeNode[] nodes)
    {
        NodeCollection.AddRange(nodes);
    }

    public ParallelQuery AsParallel()
    {
        return NodeCollection.AsParallel();
    }

    public IQueryable AsQueryable()
    {
        return NodeCollection.AsQueryable();
    }

    public IEnumerable<TResult> Cast<TResult>()
    {
        return NodeCollection.Cast<TResult>();
    }

    public void Clear()
    {
        NodeCollection.Clear();
    }

    public bool Contains(TreeNode node)
    {
        return NodeCollection.Contains(node);
    }

    public bool ContainsKey(string key)
    {
        return NodeCollection.ContainsKey(key);
    }

    public void CopyTo(Array dest, int index)
    {
        NodeCollection.CopyTo(dest, index);
    }

    public int Count
    {
        get
        {
            return NodeCollection.Count;
        }
        private set { }
    }

    public bool Equals(object obj)
    {
        return NodeCollection.Equals(obj);
    }

    public TreeNode[] Finde(string key, bool searchAllChildren)
    {
        return NodeCollection.Find(key, searchAllChildren);
    }

    public IEnumerator GetEnumerator()
    {
        return NodeCollection.GetEnumerator();
    }

    public int GetHashCode()
    {
        return NodeCollection.GetHashCode();
    }

    public Type GetType()
    {
        return NodeCollection.GetType();
    }

    public int IndexOf(TreeNode node)
    {
        return NodeCollection.IndexOf(node);
    }

    public int IndexOfKey(string key)
    {
        return NodeCollection.IndexOfKey(key);
    }

    public TreeNode Insert(int index, string text)
    {
        return NodeCollection.Insert(index, text);
    }

    public void Insert(int index, TreeNode node)
    {
        NodeCollection.Insert(index, node);
    }

    public TreeNode Insert(int index,string key, string text)
    {
        return NodeCollection.Insert(index, key, text);
    }

    public TreeNode Insert(int index, string key, string text,int imageIndex)
    {
        return NodeCollection.Insert(index, key, text, imageIndex);
    }

    public TreeNode Insert(int index, string key, string text, string imageKey)
    {
        return NodeCollection.Insert(index, key, text, imageKey);
    }

    public TreeNode Insert(int index, string key, string text, int imageIndex, int selectedImageIndex)
    {
        return NodeCollection.Insert(index, key, text, imageIndex, selectedImageIndex);
    }

    public TreeNode Insert(int index, string key, string text, string imageKey, string selectedImageKey)
    {
        return NodeCollection.Insert(index, key, text, imageKey, selectedImageKey);
    }

    public bool IsReadyOnly
    {
        get
        {
            return NodeCollection.IsReadOnly;
        }
        private set
        {
        }
    }

    public IEnumerable<TResult> OfType<TResult>()
    {
        return NodeCollection.OfType<TResult>();
    }

    public void Remove(TreeNode node)
    {
        NodeCollection.Remove(node);
    }

    public void RemoveAt(int index)
    {
        NodeCollection.RemoveAt(index);
    }

    public void RemoveByKey(string key)
    {
        NodeCollection.RemoveByKey(key);
    }

    public string ToString()
    {
        return NodeCollection.ToString();
    }
}