派生ComboBox的诡异问题
想要一个显示若干固定年份的ComboBox,所以派生了ComboBox,在构造函数里面添加年份。
然后在窗体设计器中,把YearComboBox拖到form上,结果发现,设计器生成的代码中
竟然又把这些年份添加了一遍。似乎是设计器自动解析了我的构造函数生成了相应代码。结果现在的下拉框里面年份列表被添加了两次。
请问这是什么原因呢?
C#,VS2008
代码如下:
============YearCombobox.cs===============================
class YearComboBox: ComboBox
{
public YearComboBox()
{
this.DropDownStyle = ComboBoxStyle.DropDownList;
for (int i = 1998; i < 2008; i++)
{
this.Items.Add(i);
}
this.SelectedItem = 2000;
}
int SelectYear
{
get
{
return (int)this.SelectedItem;
}
set
{
this.SelectedItem = value;
}
}
}
=========================================
==============Form1.Designer.cs=============================
private void InitializeComponent()
{
...
...
this.yearComboBox21.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.yearComboBox21.FormattingEnabled = true;
this.yearComboBox21.Items.AddRange(new object[] {
1998,
1999,
2000,
2001,
2002,
2003,
2004,
2005,
2006,
2007});
...
...
}