2011-07-06 80 views
4

我有這樣的代碼:「類型 'System.Windows.Forms.TreeNodeCollection' 有沒有構造函數中定義的」

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 

如果IM刪除此行我得到該treeCollection永遠不會分配,並將始終爲空...

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

我該如何分配treeCollection作爲一個新的TreeNodeCollection,所以我可以使用我的AddNode方法向它添加節點?

+0

你有問題嗎? –

+0

爲什麼你需要構建一個?你打算怎麼處理它? – Fantius

+0

更新了問題。 – Danpe

回答

8

TreeNodeCollection有一個內部工廠或構造函數,所以它只能被TreeView控件使用。

但是......你並不需要它。只需使用單個節點作爲根節點。然後清除其孩子

rootNode.Nodes.Clear(); 

或者如果必須,只需創建一個

List<TreeNode> 
2

看來,TreeNodeCollection不應該由用戶創建的。相反,它由TreeView類的readonly屬性「節點」公開。

從.NET 1.0開始,我認爲它是一個遺留物,從泛型不存在的時代開始,程序員必須通過暴露這樣的自定義類來強制進行強類型化。

今天,更好的設計可能會暴露IList<TreeNode>甚至IDictionary<String, TreeNode>。相反,如果你想自己存儲樹節點,您可以使用一個List<TreeNode>Dictionary<String, TreeNode>,這取決於你想如何訪問節點...

本人來說,我更願意創造System.Collections.ObjectModel.KeyedCollection<String, TreeNode>定製版本(它仍然保持插入順序中的節點,但也允許鍵入訪問)。你的milleage可能會有所不同。

-1

class 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(); 
    } 
} 
-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(); 
    } 
} 
+0

這不回答問題。你的包裝類不會創建一個新的集合。它在框架類中不添加任何功能。 –

1

您可以獲得一個新的TreeNodeCollection這樣:

public TreeNodeCollection NewTreeNodeCollection(){ 
    TreeView tmp = new TreeView(); 
    return tmp.Nodes; 
} 
相關問題