2012-01-31 81 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