如何覆盖TreeNodeCollection.Add(TreeNode)? [英] How do I override TreeNodeCollection.Add(TreeNode)?

查看:51
本文介绍了如何覆盖TreeNodeCollection.Add(TreeNode)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个:

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);       
}

我想做的是 Override Add 方法,这样我就可以递增/递减属于 DRT_Tvw_Abstract 类的一部分的自定义整数属性同时我添加一个 TreeNode .我首先尝试从 TreeNodeCollection 类派生,但这似乎是死路一条,因为我无法弄清楚 TreeNodeCollection 类的有效ctor是什么

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 (如 Nodes.Add 中)是一个属性.我如何从中衍生出来,以便覆盖 Add 方法?我首先创建了此特性的替代,但是对于如何从属性替代代码中替代方法( Add ),我一无所知.

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
    {
    }
}

重写TreeNodeCollection类的Add(TreeNode)方法的语法是什么?

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

推荐答案

Add 方法属于

  • 自然地,您可能会考虑从 TreeNodeCollection 派生并定义新方法,并从 TreeView 派生并将其 TreeNodeCollection 替换为新的自定义节点集合.但是不要尝试此解决方案. TreeNodeCollection 没有公共的构造函数.因此,我建议创建

    您可能想到的另一种解决方案是从 TreeView 派生并将新的 Add 方法添加到派生的 TreeView .

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

    示例-扩展方法

    创建一个新的代码文件,然后复制以下代码并将其粘贴到该代码文件中:

    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;
            }
        }
    }
    

    然后在要使用新的 Add 方法的类中,首先添加using语句:

    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);
    

    这篇关于如何覆盖TreeNodeCollection.Add(TreeNode)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆