如何为派生控件的属性设置默认值?

发布于 2025-01-01 05:48:19 字数 1149 浏览 4 评论 0 原文

我从 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%"});

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夏至、离别 2025-01-08 05:48:19

由于它是用户刚刚从中选择的派生列表,因此尝试将其添加到派生组合框中以防止项目序列化:

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

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:

[Browsable(false)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public new ObjectCollection Items
{
  get { return ((ComboBox)this).Items; }
}
策马西风 2025-01-08 05:48:19

也许您应该仅在不处于设计模式时才进行添加,例如:

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

Perhaps you should do the adding only when you're NOT in design mode, for example:

if (this.DesignMode)
{
  // design time only stuff
}
else
{
  // runtime only stuff.
  foreach (int i in percentages)
  {
    ComboBox.Items.Add(String.Format("{0}%", i));
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文