C#:当项目是UserControls时ComboBox中的错误? [英] C#: Bug in ComboBox when items are UserControls?

查看:179
本文介绍了C#:当项目是UserControls时ComboBox中的错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我将从UserControl派生的类中的对象添加到ComboBox控件。这是真正有用的,因为我可以直接从组合框访问必要的控制。

除了所有ComboBox条目都是空字符串的事实,所有的工作正常(后面的派生UserControls可以使用selectedItem完全访问) ...

ComboBox使用DropDownList作为其下拉式样式 - 但是更改不会修复它。


I am adding objects from classes which are derived from UserControl to a ComboBox control. This is really helpful as I can access the necessary control directly from the combobox.
It all works fine EXCEPT for the fact that all ComboBox entries are empty strings (the derived UserControls behind it are fully accessible using selectedItem)...
The ComboBox uses DropDownList as its drop down style - but changing that doesn't fix it.

字符串:

public class TestControl : UserControl {
    public override string toString(){
        return "Example";
    }
}

...
combobox.Items.Add(new TestControl());
...

当我调用

combobox.Items.Add(new TestControl().ToString());

,条目为Example。

directly, the entry is "Example".

这是ComboBox控件中的错误还是我做错了?
谢谢

Is this a bug in the ComboBox control or am I doing something wrong? Thank you

推荐答案

奇怪,这应该可以工作。另一个选择是将组合框的DisplayMember属性设置为TestControl上的属性:

Odd, that should work. Another alternative would be to set the DisplayMember property of the combobox to a property on your TestControl:


通常, ComboBox是该对象的ToString方法返回的
字符串。如果你想有
a成员显示对象,选择将
显示的成员通过设置DisplayMember属性为
适当的成员的名称。

Typically, the representation of an object in the ComboBox is the string returned by that object's ToString method. If you want to have a member of the object displayed instead, choose the member that will be displayed by setting the DisplayMember property to the name of the appropriate member.

http://msdn.microsoft.com/en-us/library/system.windows.forms.combobox.items.aspx (在备注部分)

这篇关于C#:当项目是UserControls时ComboBox中的错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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