从ComboBox派生 [英] Deriving from ComboBox

查看:122
本文介绍了从ComboBox派生的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从ComboBox派生一个类,并更改其Items属性。这是我的代码:

I need to derive a class from ComboBox and change its Items property. Here is my code:

public class MyComboBox2 : ComboBox
{
    private MyObjectCollection MyItems;

    public MyComboBox2()
    {
        MyItems = new MyObjectCollection(this);
    }

    //new public ComboBox.ObjectCollection Items
    new public MyObjectCollection Items
    {
        get {
            return MyItems;
        }
    }


}

public class MyObjectCollection : ComboBox.ObjectCollection
{
    public MyObjectCollection(ComboBox Owner) : base(Owner)
    {

    }

    new public int Add(Object j)
    {
        base.Add(j);
        return 0;
    }


}

我创建了一个新类 MyComboBox2 派生自 ComboBox 。这个类应该有一个新的Items属性,它将是 MyObjectCollection ,而不是 ComboBox.ObjectCollection 。我在类型 MyComboBox2 的形式上有一个名为 myComboBox21 的comboBox。当我想添加一个新的对象到我的ComboBox,我将执行代码如下: myComboBox21.Items.Add(text);
在这种情况下,我最终执行自己实现的 MyObjectCollection Add 方法。但是,窗体上的ComboBox最终不包含值'text'。我附加调试器的屏幕截图显示ComboBox值。 MyComboBox21 包含项目属性(它包含text,如截图2.png所示)它包含 base.Items (它不包含文本,如1.png所示)。所以,显然, MyComboBox21 包含自己的 Items 属性(我可以插入到),其基类的 Items 属性,它显示在Windows窗体中。我能做什么,以便我可以用我自己的方法成功添加到comboBox?因为我的ComboBox有2个Items属性,我可以指定哪些Items属性的值应该显示在ComboBox?


As you can see, I am creating a new class MyComboBox2 derived from ComboBox. This class is supposed to have a new Items property, which would be of type MyObjectCollection rather than ComboBox.ObjectCollection. I have a comboBox called myComboBox21 on the form of type MyComboBox2. When I want to add a new object to my ComboBox, I would execute code like this: myComboBox21.Items.Add("text"); In this case, I end up executing the Add method of MyObjectCollection that I implemented myself. However, the ComboBox on the form does not end up containing value 'text'. I am attaching screenshot of debugger showing ComboBox values. MyComboBox21 contains Items Property (which does contain "text", as shown in screenshot "2.png"), and it contains base.Items (which does not contain "text" as shown in "1.png"). So, apparently, MyComboBox21 contains its own Items property (which I can insert to), and its base class's Items property, which gets displayed in the Windows Form. What can I do so that I can successfully add to comboBox with my own method? Since my ComboBox has 2 Items properties, can I specify which Items property's values should be shown in ComboBox?

推荐答案

只需快速查看代码即可:

Just by looking very quickly on the code:

原始项索引声明为

     virtual Object this[int index] {...}

关键字是否可以通过覆盖要使运行时选择所需的代码?

Does the new keyword maybe be exchanged by override in your implementation in order to make the runtime pick the intended code?

这篇关于从ComboBox派生的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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