Windows Develop Bookmark and Share   
 index > Windows Forms Designer > Default values of control properties
 

Default values of control properties

Hi all,

I don't understand how to correctly implement default values for controls properties, and can't find anything in the docs or any of my manuals about it!  I have something like:
<CODE>

private string _HeaderText = "MyControl";

[DefaultValue(typeof(string),"MyControl")]
public string HeaderText
{
  get{return _HeaderText;}
  set{_HeaderText = value;
  drawHeader();}
}
</CODE>

This works in terms of the default value coming through to the designer, and I can change the text in the designer and it updates fine, however the actual run time interface ignores the settings set at design time and just goes back to "MyControl".  

If I do not set the private _HeaderText = "MyControl" (and simply declare it) then the default value does not come through to the designer.

I'm confused because I thought that the [DefaultValue("value")] is what is used for the designer value. ???  

Help!

MigrationUser 1  Thursday, September 25, 2003 11:46 AM
The DefaultAttribute controls the appearance of the property value in the property grid.  If the initial value of the property matches the default value, then the value is in normal task.  However, if it's different, then it appears bolded.  The DefaultValue attribute doesn't control anything related to designer serialization.

As far as why your design time values aren't being serialized, I would change the way you are passing the DefaultValue to the attribute:

[DefaultValue("MyControl")]


...should work just fine.
MigrationUser 1  Thursday, September 25, 2003 3:20 PM
Thanks for straitening me out about the default value...

However it still doesn't work.  Now I have:
<CODE>
private string _HeaderText = "MyControl";

[DefaultValue("MyControl")]
public string HeaderText

{

  get{return _HeaderText;}

  set{_HeaderText = value;

  drawHeader();}

}
</CODE>
When I first drop my control on the form it appears correctly with the text, "MyControl".  I then change this in the property grid to say "Hello"... The design time representation of the control updates correctly with the text , "Hello".

However if I hit F5 and run it, the control displays "MyControl".  

Am I implementing this wrong?  Should I not be setting the private fields value?  What gives?

MigrationUser 1  Thursday, September 25, 2003 6:38 PM
hrm... This is strange as the value should be serializing appropriately.  After you change the property value, switch to code view and look in the auto-generated section of code.  Ensure that you see the new property value in that region.  If you don't, apply the following attribute to your property:

[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
MigrationUser 1  Friday, September 26, 2003 2:53 PM
I finally figured it out... All that was wrong was that I had changed the buld configuration so that it wasn't building the windows app, only the component.... DUH!  So when I was running it, my settings were getting updated... I am so smart.

Thanks!
MigrationUser 1  Monday, September 29, 2003 12:44 PM
LOL... At least you finally got it working.
MigrationUser 1  Monday, September 29, 2003 3:41 PM

You can use google to search for other answers

Custom Search

More Threads

• CollectionEditors & ComponentDesigners
• is there way to display the custom class is not inherited component?
• How do I abort or cancel a Text Box input.
• Nested datagrid views?
• DataGridViewComboBox Column DataBinding
• SmartTags on root component?
• Bulk of Controls Created at Runtime in Windows Application?
• UserControl basics
• Creating windows child form
• Can Toolstrip inherited to be changed?