开发者

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

开发者 https://www.devze.com 2023-03-18 21:49 出处:网络
I have this code: private TreeNodeCollection treeCollection; public Cl开发者_如何转开发ient(TcpClient c)

I have this code:

    private TreeNodeCollection treeCollection;

    public Cl开发者_如何转开发ient(TcpClient c)
    {
        this.tcpClient = c;
        this.tree = null;
        this.treeCollection = new TreeNodeCollection();
        this.treeCollection.Clear();
    }

    public void AddNode(string node)
    {
        this.treeCollection.Add(node);
    }

the this.treeCollection = new TreeNodeCollection(); returns

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

And if im deleting the this row i get that treeCollection is never assigned and will always be null...

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

How can i assign the treeCollection as a new TreeNodeCollection so i can add nodes to it using my AddNode method?


TreeNodeCollection has an internal factory or constructor, so it can only be used by the TreeView control.

But... you don't need it. Just use a single node as your root node. Then clear its children with

rootNode.Nodes.Clear();

Or if you must, just create a

List<TreeNode>


It seems that the TreeNodeCollection isn't supposed to be created by a user. Instead, it is exposed by the readonly property "Nodes" of the TreeView Class.

Seeing that it's available since .Net 1.0, I consider it a relic from a time where generics didn't exist and programmers had to enforce strong typing by exposing such custom classes.

Today, a better design would probably expose an IList<TreeNode> or even an IDictionary<String, TreeNode>. Conversely, if you want to store TreeNodes yourself, you may use a List<TreeNode> or a Dictionary<String, TreeNode>, depending on how you want to access your nodes...

Personaly, I'd prefer to create a custom version of System.Collections.ObjectModel.KeyedCollection<String, TreeNode> (it would still keep the nodes in the insertion order but would also allow for keyed access). Your milleage may vary.


You can obtain a new TreeNodeCollection this way:

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


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

精彩评论

暂无评论...
验证码 换一张
取 消