如何让.Net控件在设计时InitializeComponent()中不生成相关代码
作者:fuyun 日期:2006-04-08
/// 控制器通讯类型下拉列表框。
/// </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; }
}
}
// 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);
"串口",
"TCP",
"串口",
"TCP",
"串口",
"TCP",
"串口",
"TCP",
"串口",
"TCP"});
指定某个属性 (Property) 是否只能在设计时设置。
通过将 DesignOnlyAttribute 设置为 true 进行标记的成员只能在设计时进行设置。通常,这些属性 (Property) 只能在设计时存在,并且不对应于运行时对象上的某个实际属性 (Property)。
没有属性 (Attribute) 或通过将 DesignOnlyAttribute 设置为 false 进行标记的成员可以在运行时进行设置。默认为 false。
将CommunicationTypeComboBox的Items属性加上DesignOnlyAttribute 就可以完美解决该问题。
/// <summary>
/// 获取列表框中的所有项。
/// </summary>
[DesignOnly(false)]
public new ObjectCollection Items
{
get { return base.Items; }
}
2006-9-15 日更新
终极及解决方案
/// <summary>
/// 获取列表框中的所有项。
/// </summary>
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
[Browsable(false)]
public new ObjectCollection Items
{
get { return base.Items; }
}
Tags: 属性 DesignOnly







