如何隐藏在定制ComboBox中的项目属性 [英] How to hide the Items property in custom ComboBox

查看:329
本文介绍了如何隐藏在定制ComboBox中的项目属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建从组合框继承的自定义控制。我需要当一个项目添加到组合框进行自定义操作。没关系,如果是C#或Vb.NET,但我不知道该怎么办了。



看起来像它不值得修改的一切努力检测到它,因为在这里看到:结果
事件检测项目添加到组合框



因此,也许可以创建我的自定义。新增(); 方法,但后来我需要隐藏产品属性,以便控制不允许添加的两种不同的方法和只有一个工作。



我试过的此线程,但我没有管理,使其工作。例如,我试图把超载的财产私人,但并没有改变什么,在Vb.NET在这个例子中看出:



<预类=朗动prettyprint-覆盖> 专用重载ReadOnly属性项目()作为ObjectCollection
'...

能不能做到?


解决方案

是,没有。



当然可以的隐藏原项目集合和替换它的的东西其他。



不过新的自定义的项目将无法为工作;它始终是空的 - 至少这是我发现的。 ?也许有人可以用的为什么..



这是细节跳,为什么我的东西,而没用取代它 - 一个裸体的对象



您有什么做的是:你可以暴露最初的项目在一个人在这方面的中间人类和自定义ComboBox类使用它。你可以如命名曝光类Items_并创建一个类来代替最终继承级别的项目。这个类需要实现您希望自定义类的消费者有所有方法和属性。



我点名要自定义项目项目和实施一种方法和一个属性。
(对于不区分大小写的VB也许Itemz会更好; - )



下面是最小的人在这方面的中间人:



  [的ToolboxItem(假)] 
公共类mimComboBox:组合框
{
公共ObjectCollection items_;

公共mimComboBox()
{
items_ =项目;
}

}

这是一个自定义组合框:

 公共类myComboBox:mimComboBox 
{
公共myComboBox()
{
=项新的对象(); //(1)
项=新myItems(本);
}

新的公共对象的项目{搞定;组; } //这个隐藏真正的项目(2)
公共myItems项目{搞定;组; } //这是自定义属性

公共类myItems //自定义类的项目与所有必要的方法等。
{
公共myItems(myComboBox父)
{
parentCB =父母; //参考外部类
}

私人mimComboBox parentCB; //的人在这方面的中间人

//所有的方法..
新的公众诠释添加(对象o)//自定义的方法
的一个例子{
//此处添加您添加的项目事件
返回parentCB.items_.Add(O);
}

//很多很多的属性之一,提供..
公众诠释计数{{返回parentCB.items_.Count; }}

}

}

这有一个例子组合框如何使用:

 私人无效的button1_Click(对象发件人,EventArgs五)
{
myComboBox1.items.Add(uiuiuiiui);
// myComboBox1.Items.Add(sadsds); //< ==这不编译! (3)
button1.Text = myComboBox1.items.Count.ToString();
}

这听起来颇有些工作!



(可能是人在的中产阶级是没有必要的..?我想了解的..!)



编辑:我根据Plutonix评论的链接已经改变了一些细节



注:




  • 如前所述,原来的问题是更好地倾听到ComboBox系统消息解决


  • 我仍然不知道为什么会这样




当我更换(1)及(2)

 产品=新myItems(本); 
新的公共myItems项目{搞定;组; }



(3)然后将编译就好了。但是在运行时,它抛出一个空对象引用错误。



这是缺少可重写属性的一个迟来的影响? @乔恩飞碟双向救援! ; - )


I am creating a custom control that inherits from ComboBox. I need to perform a custom action when a Item is added to the ComboBox. It doesn't matter if it's C# or Vb.NET, but I don't know how to do it.

Looks like it's not worth the effort to modify everything to detect it, as seen here:
Event to detect item added to ComboBox

So maybe is possible to create my custom .Add(); method, but then I need to hide the Items property so the control doesn't allow two different method of adding and only one working.

I tried using things in this thread but I didn't manage to make it work. For example, I tried putting the overloaded property private, but didn't change anything, as seen in this example in Vb.NET:

Private Overloads ReadOnly Property Items() As ObjectCollection
'...

Can it be done?

解决方案

Yes and no.

Yes you can hide the original Items collection and replace it by something else.

However your new 'custom' Items won't be able to work; it is always null - at least that's what I found. Maybe somebody can jump in with details of the why..?

Which is why I replace it with something rather useless - a naked object.

What you can do is this: You can expose the original Items in a man-in-the-middle class and use it in your custom ComboBox class. You could e.g. name the exposed class Items_ and create a class to replace the Items in the final inheritance level. This class needs to implement all methods and properties you want your custom class's consumers to have.

I have named to custom Items 'items' and implemented one method and one property. (For non-case sensitive VB maybe Itemz would be better ;-)

Here is the minimal man-in-the-middle :

[ToolboxItem(false)]
public class mimComboBox : ComboBox
{
    public ObjectCollection items_;

    public mimComboBox()
    {
        items_ = Items;
    }

}

And here is a custom ComboBox:

public class myComboBox : mimComboBox
{
    public myComboBox()
    {
        Items = new object();               // (1)
        items = new myItems(this);
    }

    new public object Items { get; set; }  // this hides the real Items  (2)
    public myItems items { get; set; }     // this is the custom property

    public class myItems // the custom Items class with all necessary methods etc..
    {
        public myItems( myComboBox parent )
        {
            parentCB = parent;  // reference to the outer class
        }

        private mimComboBox parentCB;  // the man-in-the-middle

        // all your methods..
        new public int Add(object o)  // one example of a custom method
        {
            // add your item-added event here
            return parentCB.items_.Add(o);
        }

        // one of many many properties  to provide..
        public int Count { get { return parentCB.items_.Count; } } 

    }

}

This is an example how the ComboBox can be used:

private void button1_Click(object sender, EventArgs e)
{
    myComboBox1.items.Add("uiuiuiiui");
    // myComboBox1.Items.Add("sadsds");  // <== this doesn't compile! (3)
    button1.Text = myComboBox1.items.Count.ToString();
}

This sounds like quite some work!!

(Maybe the man-in-the-middle class is not necessary..? I'd like to learn about that..!)

Edit: I have changed a few details according to the link in Plutonix' comment.

Notes:

  • As noted, the original question is better solved by listening to the ComboBox system messages

  • I still wonder why this happens:

When I replace (1) & (2) by

Items = new myItems(this);
new public myItems Items { get; set; } 

then (3) will compile just fine. However upon runtime it throws a null object reference error.

Is this a belated effect of the missing Overridable attribute? @Jon Skeet to the rescue! ;-)

这篇关于如何隐藏在定制ComboBox中的项目属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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