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が割り当てられることはなく、常にnullになります...

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 クラスの読み取り専用プロパティ「Nodes」によって公開されます。

.Net 1.0 以降で使用できるようになったことを考えると、ジェネリックが存在せず、プログラマーがそのようなカスタム クラスを公開して強力な型指定を強制しなければならなかった時代の遺物だと思います。

今日、より良い設計では、おそらく 、IList<TreeNode>またはIDictionary<String, TreeNode>. 逆に、TreeNode を自分で保存する場合は、ノードへのアクセス方法に応じて、List<TreeNode>またはを使用できます...Dictionary<String, TreeNode>

個人的には、のカスタム バージョンを作成することをお勧めしますSystem.Collections.ObjectModel.KeyedCollection<String, TreeNode> (ノードを挿入順序で保持しますが、キー付きアクセスも許可します)。あなたの走行距離は異なる場合があります。

于 2011-07-06T17:08:02.393 に答える
-1

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();
    }
}
于 2013-05-06T20:22:57.240 に答える