2012-01-31 98 views
1

我从ToolStripComboBox派生出来以创建一个封装百分比挑选下拉菜单的工具。目标是在派生控件中进行所有验证和字符串解析。当所选择的百分比发生变化时,家长只会收到一个事件,并且可以访问用于获取和设置百分比的公共整数。如何为派生控件的属性设置默认值?

我的问题是,在我把我的派生的控件父控件设计文件时,它通常会持续增加全套使用ComboBox.Items.AddRange方法字符串。在构造函数中我得到的控制,我有以下:

foreach (int i in percentages) 
{ 
    ComboBox.Items.Add(String.Format("{0}%", i)); 
} 

随着时间的推移,这些值在设计文件过很多很多次积累。我不知道如何使Items属性隐藏,因为它不是虚拟的。我想压制我的设计器文件的洪水。我的设计文件的

例子:

this.zoom_cbo.Items.AddRange(new object[] { 
"10%", 
"25%", 
"50%", 
"75%", 
"100%", 
"150%", 
"200%", 
"300%", 
"400%", 
"600%", 
"800%", 
"1600%", 
"10%", 
"25%", 
"50%", 
"75%", 
"100%", 
"150%", 
"200%", 
"300%", 
"400%", 
"600%", 
"800%", 
"1600%", 
"10%", 
"25%", 
"50%", 
"75%", 
"100%", 
"150%", 
"200%", 
"300%", 
"400%", 
"600%", 
"800%", 
"1600%", 
"10%", 
"25%", 
"50%", 
"75%", 
"100%", 
"150%", 
"200%", 
"300%", 
"400%", 
"600%", 
"800%", 
"1600%", 
"10%", 
"25%", 
"50%", 
"75%", 
"100%", 
"150%", 
"200%", 
"300%", 
"400%", 
"600%", 
"800%", 
"1600%", 
"10%", 
"25%", 
"50%", 
"75%", 
"100%", 
"150%", 
"200%", 
"300%", 
"400%", 
"600%", 
"800%", 
"1600%", 
"10%", 
"25%", 
"50%", 
"75%", 
"100%", 
"150%", 
"200%", 
"300%", 
"400%", 
"600%", 
"800%", 
"1600%"}); 

回答

1

因为它是一个衍生列表中用户只是从中选择,尝试将其添加到派生组合框以防止项目的序列化:

[Browsable(false)] 
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)] 
public new ObjectCollection Items 
{ 
    get { return ((ComboBox)this).Items; } 
} 
1

也许你应该做的,只有当你在设计模式又不是加入,例如:

if (this.DesignMode) 
{ 
    // design time only stuff 
} 
else 
{ 
    // runtime only stuff. 
    foreach (int i in percentages) 
    { 
    ComboBox.Items.Add(String.Format("{0}%", i)); 
    } 
} 
+0

T他没有为我工作。 – 2012-01-31 17:57:36