How do I override TreeNodeCollection.Add(TreeNode)?

久未见 提交于 2019-12-08 01:28:23

问题


I have this:

public abstract class DRT_Tvw_Abstract : TreeView
{
    TableCountMetrics metrics;
    public TableCountMetrics Metrics { get => metrics; set => metrics = value; }

    public class TableCountMetrics
    {
        int schemaNameCount = 0;
        int tableNameCount = 0;

        public int SchemaNameCount { get => schemaNameCount; set => schemaNameCount = value; }
        public int TableNameCount { get => tableNameCount; set => tableNameCount = value; }
    }

    public DRT_Tvw_Abstract() : base()
    {
    }
}

public class DRT_Tvw_TableList_Unfiltered : DRT_Tvw_Abstract
{
    public DRT_Tvw_TableList_Unfiltered() : base()
    {
    }

    public void _CreateTreeView(DataTable dataTable)
    {
        tableListTreeViewNode = new {treeview node from my class that derives from TreeNode}

        Nodes.Add(tableListTreeViewNode);       
}

What I want to do is override the Add method so I can increment/decrement custom integer properties that are part of my DRT_Tvw_Abstract class at the same time I Add a TreeNode. I started by trying to derive from the TreeNodeCollection class, but that seems to be a dead-end because I can't figure out what a valid ctor is for the TreeNodeCollection class

Nodes (as in Nodes.Add) is a property. How do I derive from it so I can override the Add method? I started by creating an override for this poprperty, but I have no clue as to how you override a method (Add) from within property override code.

public new virtual TreeNodeCollection Nodes
{

    //override the Add method from within here somehow?

    get
    {
        return base.Nodes;
    }

    set
    {
    }
}

What is the syntax for overriding the TreeNodeCollection class Add(TreeNode) method?


回答1:


Add method belongs to TreeNodeCollection class. If you like to have new Add methods with new signatures:

  • Naturally you may think about deriving from TreeNodeCollection and defining new methods and derive from TreeView and replace its TreeNodeCollection with your new custom node collection. But don't try this solution. TreeNodeCollection doesn't have a public constructor. So I'd suggest creating Extension Methods.

  • The other solution that you may think about it is deriving from TreeView and adding new Add methods to the derived TreeView.

Example - Extension Methods

Create a new code file and copy the following code and paste it in the code file:

namespace MyExtensions
{
    using System.Windows.Forms;
    public static class TreeNodeCollectionExtensions
    {
        //Just an example of a new signature, use the signature that you need instead:
        public static TreeNode Add(this TreeNodeCollection nodes, string text, int tag)
        {
            var node = nodes.Add(text);
            node.Tag = tag;
            return node;
        }
    }
}

Then in the class which you are going to use the new Add method, first add the using statement:

using MyExtensions;

Then use it like a normal method:

treeView1.Nodes.Add("One", 1);


来源:https://stackoverflow.com/questions/51089409/how-do-i-override-treenodecollection-addtreenode

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!