4

我有这个代码:

    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 方法向其中添加节点?

4

5 回答 5

9

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

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

rootNode.Nodes.Clear();

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

List<TreeNode>
于 2011-07-06T16:49:54.680 回答
3

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

看到它从 .Net 1.0 开始就可用,我认为它是泛型不存在的时代的遗物,程序员不得不通过公开此类自定义类来强制执行强类型。

今天,一个更好的设计可能会暴露一个IList<TreeNode>甚至一个IDictionary<String, TreeNode>. 相反,如果你想自己存储 TreeNodes,你可以使用 aList<TreeNode>或 a Dictionary<String, TreeNode>,这取决于你想如何访问你的节点......

就个人而言,我更喜欢创建一个自定义版本System.Collections.ObjectModel.KeyedCollection<String, TreeNode> (它仍将节点保持在插入顺序中,但也允许键控访问)。您的里程可能会有所不同。

于 2011-07-06T17:08:02.393 回答
1

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

public TreeNodeCollection NewTreeNodeCollection(){
    TreeView tmp = new TreeView();
    return tmp.Nodes;
}
于 2013-11-29T20:45:18.313 回答
-1

类 UGTreeNodeCollection { 私有 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();
    }
}
于 2013-05-06T20:19:10.877 回答
-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();
    }
}
于 2013-05-06T20:22:57.240 回答