C# Creating Custom Controls
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 ...
[ReadOnly(true)]
public override string Text
{ get { return "&Exit Application"; } }
Never have to worry about a "setter" since the button's text should never change in the designer or derived...
EDIT -- more complete sample...
public class MyButton : Button
{
[ReadOnly(true)]
public sealed override Font Font
{ get { return new Font("Arial", 9F, FontStyle.Italic, GraphicsUnit.Point); } }
[ReadOnly(true)]
public sealed override Color ForeColor
{ get { return Color.Blue; } }
public GDITButton()
{ }
}
public class MyExitButton : MyButton
{
[ReadOnly(true)]
public override string Text
{ get { return "Exit"; } }
public MyExitButton()
{}
}
public class MyAddButton : MyButton
{
[ReadOnly(true)]
public override string Text
{ get { return "Add"; } }
public MyAddButton()
{}
}
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.
public StandardExitButton()
{
this.Text = "Exit Application";
}
the ControlAdded event handler doesn't work the way you think, please refer to this example
does this work?
ControlAdded will fire when a control is added to your control - not when your control is added to another control. (You could try the ParentChanged event instead.)
You have to do 2 things:
Change Text property to private (to prevent setting it):
private new string Text
{
get
{
return base.Text;
}
set
{
base.Text = value;
}
}
Set default text:
public StandardExitButton()
{
this.Text = "Exit Application";
}
You probably need to override Buttons public override string Text { get; set; }
Property
Ahh - the problem here is the designer system does the following:
- Creates an instance of your control (executing the code in the constructor), so your text is "Exit Application" for a very short time.
- Next no matter what you set the text property to, the designer then explicitly sets the text to the name of the control.
I've solved this when I was building a custom forms designer (which should be out of the scope of what you're doing). I can't think of a way off the top of my head to override that behavior...
How about something like
[DefaultValue("Exit Application")]
public override string Text
{
get
{
return base.Text;
}
set
{
base.Text = value;
}
}
You can't see it by using the visual studio designer, instead you need to load it with the code and run your application to see the Text as you specified.
精彩评论