我从 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%"});
I derived from a ToolStripComboBox in order to create one that encapsulates a percentage picking dropdown. The goal is to have all of the validation and string parsing in the derived control. The parent simply receives an event when the percentage chosen has changed, and it can access a public integer for getting and setting the percentage.
The problem I have is that in the designer file for the parent control which I place my derived control on, it constantly keeps adding a full set of strings using the ComboBox.Items.AddRange method. In the constructor for my derived control I have the following:
foreach (int i in percentages)
{
ComboBox.Items.Add(String.Format("{0}%", i));
}
Over time these values accumulate in the designer file many, many times over. I don't know how to make the Items property hidden since it's not virtual. I want to suppress this flooding of my designer file.
Example of my designer file:
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%"});
发布评论
评论(2)
由于它是用户刚刚从中选择的派生列表,因此尝试将其添加到派生组合框中以防止项目序列化:
Since it's a derived list that the user is just selecting from, try adding this to your derived combo box to prevent the serialization of the items:
也许您应该仅在不处于设计模式时才进行添加,例如:
Perhaps you should do the adding only when you're NOT in design mode, for example: