类型'System.Windows.Forms.TreeNodeCollection'没有定义的构造函数
本文关键字:定义 构造函数 Forms System Windows 类型 TreeNodeCollection | 更新日期: 2023-09-27 18:01:27
我有这样的代码:
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方法?
TreeNodeCollection有一个内部工厂或构造函数,所以它只能被TreeView控件使用。
但是…你不需要它。只需使用单个节点作为根节点。然后用
清除它的子元素rootNode.Nodes.Clear();
或者,如果你必须创建一个
List<TreeNode>
似乎TreeNodeCollection不应该由用户创建。相反,它是由TreeView类的只读属性"Nodes"公开的。
看到它从。net 1.0开始可用,我认为它是泛型不存在的时代的遗物,程序员必须通过暴露这样的自定义类来强制强类型。
今天,一个更好的设计可能会暴露IList<TreeNode>
甚至IDictionary<String, TreeNode>
。相反,如果你想自己存储TreeNodes,你可以使用List<TreeNode>
或Dictionary<String, TreeNode>
,这取决于你想如何访问你的节点…
就我个人而言,我更喜欢创建一个自定义版本的System.Collections.ObjectModel.KeyedCollection<String, TreeNode>
(它仍然会保持节点的插入顺序,但也允许键控访问)。您的里程可能会有所不同。
您可以通过以下方式获得一个新的TreeNodeCollection:
public TreeNodeCollection NewTreeNodeCollection(){
TreeView tmp = new TreeView();
return tmp.Nodes;
}
公共类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();
}
}
class UGTreeNodeCollection{private TreeNodeCollection
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();
}
}