我有一个包含组合框的用户控件。我希望能够编辑组合框的Items属性,但我不确定如何做到这一点。我已经尝试将Items属性添加到我的用户控件类,但我不确定在visual studio的属性菜单中设置属性时返回的值是什么。我有这样的属性设置:
[Editor("System.Windows.Forms.Design.StringCollectionEditor, System.Design",
"System.Drawing.Design.UITypeEditor, System.Drawing")]
public ComboBox.ObjectCollection Items
{
get
{
return this.comboBox.Items;
}
set
{
this.comboBox.Items.Add(value);
}
}
答案 0 :(得分:4)
将UserControl的ComboBox的Items属性包装在如下属性中:
[Description("The items in the UserControl's ComboBox."),
DesignerSerializationVisibility(DesignerSerializationVisibility.Content),
Editor("System.Windows.Forms.Design.StringCollectionEditor, System.Design", typeof(System.Drawing.Design.UITypeEditor))]
public System.Windows.Forms.ComboBox.ObjectCollection MyItems {
get {
return comboBox1.Items;
}
}
属性中的EditorAttribute指定用于更改设计器中属性的UI元素。
答案 1 :(得分:0)
尝试添加两种方法来添加和删除ComboBox
项:
public void AddItem(Object item)
{
this.comboBox.Items.Add(item);
}
public void RemoveItem(Object item)
{
this.comboBox.Items.Remove(item);
}