I am having a go at creating some custom controls based on specific configurations that I use.
My first one, is simply to create a standard exit button. However, it’s not working how I think. Essentially, I want the default text to be “Exit Application”, when it is added to the form. Unfortunately, the two ways that I have thought of, dont actually show that, instead it shows the name of the button class, i.e. “CustomExitButton1”. This is the two ways I have tried:
public class StandardExitButton : Button
{
public StandardExitButton()
{
this.ControlAdded += new ControlEventHandler(StandardExitButton_ControlAdded);
}
void StandardExitButton_ControlAdded(object sender, ControlEventArgs e)
{
Text = "Exit Application";
}
}
And:
public class StandardExitButton : Button
{
public StandardExitButton()
{
this.Text = "Exit Application";
}
}
I’ve done this with a bunch of buttons, such as Add, Edit, Delete, Cancel, Ok, Exit, etc… In addition to keeping all my buttons, labels, textboxes, etc with my own standard font sizes, colors, etc … For the text, on each subclassed button, I did something like …
Never have to worry about a “setter” since the button’s text should never change in the designer or derived…
EDIT — more complete sample…
Then, when you put an instance of the MyExitButton or MyAddButton on the form, it will FORCE the caption for you and also prevent you from changing it as it is a read-only property of the respective class.