I have a library. In the library, I have a button with a Green background color and Text as Go Green.
Now I made a winform project and dragged my Go green button in the form. On running the application, I noticed that the button color is changing to green but text is displayed as button1 (name of the class library).
My library looks like:
public class button : Button
{
public Seats()
{
button.BackColor = Color.Green;
button.Text = "Go Green";
}
}
I discovered that it is happening because InitializeComponent() method is called in the constructor of the form. And in designer.cs,
button.Text = "button1";
is called. How can I avoid that to happen. I want my text to be visible from my class library.
Note: When I manually removed the above code from the designer.cs, everything was working fine.
Easiest way – override button’s Text property and make it hidden to designer serialization:
Designer will add default button name, but when you build application, your text will be shown.
UPDATE: Another (harder) way – provide to designer default property value for your button. In this case you need reference System.Design.dll which is available only for full version of .net framework (not client profile version).
First: create control designer for your button
Last: add Designer attribute to your custom button class
That’s it. Now when you drag button to form, it will have default text “Go Green” without any additional compilations.