How to set the DefaultValue for a Color property in a control

G

Guest

Hi,

I'm doing a control that has a custom color property and i want to set a
default value. I tried several combinations using the DefaultValue atribute
but it didn't work. It must be a constant, so i can't do:

[DefaultValue(Color.Red)]
public Color MyColor { ... }

I also tried:

[DefaultValue(typeof(Color), "255; 0; 0")]
public Color MyColor { ... }

and it didn't work.

Thanks in advance,

Bernardo
 
N

Nicholas Paldino [.NET/C# MVP]

bbrik,

Have you tried:

[DefaultValue(typeof(Color), "Red")]
public Color MyColor

That should work. You need to use the string that is used in the
property grid for that value (Red is a defined color and the type converter
used for color acknowleges it).

Hope this helps.
 
G

Guest

Hi,

That worked, thanks! What is strange is that if you type "192;192;192" in
the property grid it gets converted to the color... so why the type converter
works at design time but not in the atribute?
Anyway, I get stuck with the predefined colors... It's ok because the name
Silver maps to the color i wanted, but it should accept custom colors.
What is also strange is that when I reset the color, it still gets Bold font
face on the property grid and I still can call reset again...

thanks again!

bbrik

Nicholas Paldino said:
bbrik,

Have you tried:

[DefaultValue(typeof(Color), "Red")]
public Color MyColor

That should work. You need to use the string that is used in the
property grid for that value (Red is a defined color and the type converter
used for color acknowleges it).

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

bbrik said:
Hi,

I'm doing a control that has a custom color property and i want to set a
default value. I tried several combinations using the DefaultValue
atribute
but it didn't work. It must be a constant, so i can't do:

[DefaultValue(Color.Red)]
public Color MyColor { ... }

I also tried:

[DefaultValue(typeof(Color), "255; 0; 0")]
public Color MyColor { ... }

and it didn't work.

Thanks in advance,

Bernardo
 
G

Guest

Hi,

the designer totally ignored my ShouldSerializeMaskColor property

any ideas?

thanks!

Tim Wilson said:
Use a "ShouldSerializeX" method.
http://msdn.microsoft.com/library/d...guide/html/cpconshouldpersistresetmethods.asp

--
Tim Wilson
..Net Compact Framework MVP

bbrik said:
Hi,

I'm doing a control that has a custom color property and i want to set a
default value. I tried several combinations using the DefaultValue atribute
but it didn't work. It must be a constant, so i can't do:

[DefaultValue(Color.Red)]
public Color MyColor { ... }

I also tried:

[DefaultValue(typeof(Color), "255; 0; 0")]
public Color MyColor { ... }

and it didn't work.

Thanks in advance,

Bernardo
 
T

Tim Wilson

I assume that your property is named "MaskColor" and you have a method that
looks something like this...

public bool ShouldSerializeMaskColor()
{
return maskColorValue != (default color);
}

If the code is correct then ensure that you've rebuilt the assembly and are
not still referencing an old version.

--
Tim Wilson
..Net Compact Framework MVP

bbrik said:
Hi,

the designer totally ignored my ShouldSerializeMaskColor property

any ideas?

thanks!

Tim Wilson said:
Use a "ShouldSerializeX" method.
http://msdn.microsoft.com/library/d...guide/html/cpconshouldpersistresetmethods.asp

--
Tim Wilson
..Net Compact Framework MVP

bbrik said:
Hi,

I'm doing a control that has a custom color property and i want to set a
default value. I tried several combinations using the DefaultValue atribute
but it didn't work. It must be a constant, so i can't do:

[DefaultValue(Color.Red)]
public Color MyColor { ... }

I also tried:

[DefaultValue(typeof(Color), "255; 0; 0")]
public Color MyColor { ... }

and it didn't work.

Thanks in advance,

Bernardo
 

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

Top