我通过扩展System.
Windows.Forms.Button类创建了一个自定义控件按钮.
我在新类的构造函数中设置了默认的.Text .Width和.Height.
当我将此控件放到表单上时,IDE足够聪明,可以注意构造函数中指定的宽度和高度,并将这些属性分配给正在创建的新按钮,但它会忽略Text属性,并指定.Text按钮是“ucButtonConsumables1”
有没有办法将.Text设置为我选择的默认值?
public partial class ucButtonConsumables : System.Windows.Forms.Button { public ucButtonConsumables() { this.Text = "Consumables"; this.Width = 184; this.Height = 23; this.Click += new EventHandler(ucButtonConsumables_Click); } void ucButtonConsumables_Click(object sender,EventArgs e) { MessageBox.Show("Button Clicked") } }
解决方法
从设计器序列化中隐藏Text属性:
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)] public override string Text { get { return base.Text; } set { base.Text = value; } }
或使用默认值创建设计器:
public class ConsumablesButtonDesigner : System.Windows.Forms.Design.ControlDesigner { public override void OnSetComponentDefaults() { base.OnSetComponentDefaults(); Control.Text = "Consumables"; } }
并为您的按钮提供设计师:
[Designer(typeof(ConsumablesButtonDesigner))] public class ucButtonConsumables : Button { //... }