how to set default values for non-basic type properties?

B

Ben Schwehn

Hello everyone.

I need some help with how to set default values for 'complex' properties.

My problem is this:
I have a control with a property CellStyle of type TableItemStyle. I want
to initialise this property with some default values eg.
CellStyle.ForeColor="Gray" etc.

With a basic datatype i would just use the DefaultValueAttribute eg for a
bool I'd add the attribute: [DefaultValue(true)] How can i do this for my
CellStyle property?

My workaround so far is to just inititialise CellStyle in the
contstructor, which works to the extend that the values I set in the
constructor are all displayed in VS, however I would like the IDE to know
that the values are default values (because the IDE adds all values it
thinks are not default to the tag definition in the aspx file eg adds
<CellStyle Font-Size="12px" Height="32px" BackColor="#D6D3CE"></CellStyle>
to the control tag even though these are the default values).

Thanks!
Ben
 
J

John Saunders

Ben Schwehn said:
Hello everyone.

I need some help with how to set default values for 'complex' properties.

My problem is this:
I have a control with a property CellStyle of type TableItemStyle. I want
to initialise this property with some default values eg.
CellStyle.ForeColor="Gray" etc.

With a basic datatype i would just use the DefaultValueAttribute eg for a
bool I'd add the attribute: [DefaultValue(true)] How can i do this for my
CellStyle property?

My workaround so far is to just inititialise CellStyle in the
contstructor, which works to the extend that the values I set in the
constructor are all displayed in VS, however I would like the IDE to know
that the values are default values (because the IDE adds all values it
thinks are not default to the tag definition in the aspx file eg adds
<CellStyle Font-Size="12px" Height="32px" BackColor="#D6D3CE"></CellStyle>
to the control tag even though these are the default values).

When you add a DefaultValue attribute to a property, it does not initialize
the property to that value. This attribute simply tells the Property Grid
which value is the default. All non-default values will be displayed in bold
type.
 
B

Ben Schwehn

My problem is this:
....

When you add a DefaultValue attribute to a property, it does not initialize
the property to that value. This attribute simply tells the Property Grid
which value is the default. All non-default values will be displayed in bold
type.

Hello John,

that's right. i should have said

--
With a basic datatype i would just use the DefaultValueAttribute eg for a
bool I'd add the attribute: [DefaultValue(true)] _and_ initialise it to
true.
--

Still, how do i tell the Prooperty Grid which values are default with a
Property of type TableItemStyle, is it possible at all? I was thinking
about deriving a type from TableItemStyle that initialises all properties
to the value i want and then use

MyOwnTableItemStyle bla = new MyOwnTableItemStyl();

....

DefaultValue(bla)


not sure whether that would work at all but even if it does it's quite
messy code i think...


Cheers
ben
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top