日期:2010-07-30 浏览次数:20417 次
组件的一些公共属性不希望被VS在设计时加到InitializeComponent()方法中怎么处理呢?我试过了,将属性加上[Browsable(false)]也不行。
我的代码如下:
/// <summary>
/// 控制器通讯类型下拉列表框。
/// </summary>
public class CommunicationTypeComboBox : ComboBox
{
/// <summary>
/// 构造列表框实例。
/// </summary>
public CommunicationTypeComboBox()
{
Items.Add("串口");
Items.Add("TCP");
}
/// <summary>
/// 获取列表框中的所有项。
/// </summary>
[Browsable(false)]
public new ObjectCollection Items
{
get { return base.Items; }
}
}
将控件放到窗体上,VS回自动在InitializeComponent()方法中加入一下代码。粗体部分。
//
// cmbCommunicationType
//
this.cmbCommunicationType.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.cmbCommunicationType.FormattingEnabled = true;
this.cmbCommunicationType.Items.AddRange(new object[] {
"串口",
"TCP"});
this.cmbCommunicationType.Location = new System.Drawing.Point(124, 66);
this.cmbCommunicationType.Name = "cmbCommunicationType";
this.cmbCommunicationType.SelectedItem = Xunmei.Door.CommunicationType.SerialPort;
this.cmbCommunicationType.Size = new System.Drawing.Size(121, 20);
this.cmbCommunicationType.TabIndex = 2;
this.cmbCommunicationType.SelectedIndexChanged += new System.EventHandler(this.cmbCommunicationType_SelectedIndexChanged);
随着编辑次数的增会变成这样。除了不在构造函数中增加项以外,有没有办法解决这个问题?
this.cmbCommunicationType.Items.AddRange(new object[] {
"串口",
"TCP",
"串口",
"TCP",
"串口",
"TCP",
"串口",
"TCP",