Reputation: 588
I have created a custom control button by extending the System.Windows.Forms.Button class.
I have set the default .Text .Width and .Height in the constructor of the new class.
When I drop this control onto a form, the IDE is smart enough to pay attention to the Width and Height specified in the constructor and assign these properties to the new button being created, but it ignores the Text property, and assignes the .Text of the button to be "ucButtonConsumables1"
Is there a way to set the .Text to a default value of my choosing?
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")
}
}
Upvotes: 5
Views: 3138
Reputation: 236228
Hide Text property from designer serialization:
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public override string Text
{
get { return base.Text; }
set { base.Text = value; }
}
Or create designer with default values:
public class ConsumablesButtonDesigner : System.Windows.Forms.Design.ControlDesigner
{
public override void OnSetComponentDefaults()
{
base.OnSetComponentDefaults();
Control.Text = "Consumables";
}
}
And provide that designer to your button:
[Designer(typeof(ConsumablesButtonDesigner))]
public class ucButtonConsumables : Button
{
//...
}
Upvotes: 3
Reputation: 823
Yes, it is not possible for doing it in the constructor. If you are sure that the value will not be changed again do it this way. Overriding the Text Property and returning the constant.
public override string Text
{
get
{
return "Consumables";
}
set
{
}
}
Upvotes: 0
Reputation: 1485
You have to override the Text property in derived class to change.
public override string Text { get; set; }
Upvotes: -1