Windows Develop Bookmark and Share   
 index > Windows Forms Designer > Inheritance and default Text values
 

Inheritance and default Text values

Hi,

I apologize in advance for asking a question I'm sure has been answered time and time again, but I cannot for the life of me find the answer, here or anywhere in the Interwebs.

I want to inherit a control, say a button. That particular button's text should be "Save" by default. How do I make that happen?

What I originally did was simply set the Text as "Save" in the constructor of my inherited control. My problem is that when I add this inherited control to a form, the designer sets that new button's text to the default value "SaveButton1" which is the name of the class, anda number added to it for uniqueness.

I thought of overriding the Text property and disable the setting by simply ignoring the provided value, but I don't find that to be a very clean solution. A clean solution would be to make the setter private, but I also do not know how to change the access level of an inherited property to Private. Is that even possible?

Thank you.
Billy Bob Richert  Thursday, July 02, 2009 2:52 PM
Hi Billy,

Firstly, it's default Control Designer that set the Text property to the same value of the Name property.

A simple workaround is to override the Text property and determine if it is at design time and the value to be assigned to the Text property equals to the that of the Name property. If so, don't assign the new value to the Text property. The following is a sample:

public class SaveButton:Button
{
public SaveButton()
{
this.Text = "Save";
}
[DefaultValue("Save")]
public override string Text
{
get { return base.Text;}
set
{
if(!(this.DesignTime && this.Name.Equals(value)))
{
base.Text = value;
}
}
}
}

Hope this helps.

Sincerely,
Linda Liu
Please remember to mark the replies as answers if they help and unmark them if they provide no help. end us any feedback you have about the help from MSFT at fbmsdn@microsoft.com.
Linda Liu  Monday, July 13, 2009 6:44 AM
Hi Billy,

Firstly, it's default Control Designer that set the Text property to the same value of the Name property.

A simple workaround is to override the Text property and determine if it is at design time and the value to be assigned to the Text property equals to the that of the Name property. If so, don't assign the new value to the Text property. The following is a sample:

public class SaveButton:Button
{
public SaveButton()
{
this.Text = "Save";
}
[DefaultValue("Save")]
public override string Text
{
get { return base.Text;}
set
{
if(!(this.DesignTime && this.Name.Equals(value)))
{
base.Text = value;
}
}
}
}

Hope this helps.

Sincerely,
Linda Liu
Please remember to mark the replies as answers if they help and unmark them if they provide no help. end us any feedback you have about the help from MSFT at fbmsdn@microsoft.com.
Linda Liu  Monday, July 13, 2009 6:44 AM
Works like a charm!

Thanks Linda!
Billy Bob Richert  Wednesday, July 15, 2009 1:32 PM

You can use google to search for other answers

Custom Search

More Threads

• Compiler error
• How to post an article here?
• Free controls for Windows Form
• VS 2008 Designer not executing default constructor anymore?
• Binding to Array
• complex property - serialization
• Displaying Child forms in a Master form
• UserControl and Collections
• Designing .NET control with real time interface
• Corrupt Form - Designer cannot open : help