Discussion:
How can I set the defaultValue == TRUE?
(too old to reply)
Carsten Ilwig
2009-08-07 06:38:14 UTC
Permalink
Hi,

is there a way to set the defaultvalue from a property to TRUE?

Example:

public class ComboBoxMy : System.Windows.Forms.ComboBox
{
[DefaultValue(true)]
public bool UseDefaultFont { get; set; }
}

If I place the control of my form, the property "UseDefaultFont" is "FALSE".
The value is bold, because the designer realised that "FALSE" isn't the
defaultvalue.
What I have to do, that the designer set the value of "TRUE"?

Thanks for your answers.
Carsten
Eddie
2009-08-10 07:52:27 UTC
Permalink
Post by Carsten Ilwig
is there a way to set the defaultvalue from a property to TRUE?
public class ComboBoxMy : System.Windows.Forms.ComboBox
{
[DefaultValue(true)]
public bool UseDefaultFont { get; set; }
}
What's the actual implementation of UseDefaultFont?

You probably need to ensure that you initialise it somehow to your
desired default value, something like this:

public class DerivedComboBox: ComboBox
{
private bool _useDefaultFont = true;

[DefaultValue( true )]
public bool UseDefaultFont
{
get { return _useDefaultFont; }
set { _useDefaultFont = value; }
}
}
--
Eddie ***@deguello.org
Jeff Johnson
2009-08-11 13:48:07 UTC
Permalink
Post by Carsten Ilwig
If I place the control of my form, the property "UseDefaultFont" is "FALSE".
The value is bold, because the designer realised that "FALSE" isn't the
defaultvalue.
And you've discovered the use of the DefaultValue attribute. Its main use is
to tell the PropertyGrid control how to display a value to give a visual
indication on when a property has been changed from its default value. (It's
also used when you choose Reset from the context menu.) As Eddie said, to
actually SET the value you need to write some code.
Carsten Ilwig
2009-08-11 20:49:29 UTC
Permalink
Hi,

ok, I understand.
My mistake was, that I thought the DefaultValue attribute set the default
value of the property.
Now it's clear.

Thank's a lot to you.
Carsten
Post by Carsten Ilwig
Hi,
is there a way to set the defaultvalue from a property to TRUE?
public class ComboBoxMy : System.Windows.Forms.ComboBox
{
[DefaultValue(true)]
public bool UseDefaultFont { get; set; }
}
If I place the control of my form, the property "UseDefaultFont" is "FALSE".
The value is bold, because the designer realised that "FALSE" isn't the
defaultvalue.
What I have to do, that the designer set the value of "TRUE"?
Thanks for your answers.
Carsten
Loading...