How to deserialize a SettingsProperty

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,

I don't know how to retrieve the default value of user properties.

For instance, I have defined 2 user properties named TheColor and TheFont,
with the type System.Drawing.Color and System.Drawing.Font. I can access
their current values like this:

dim theColor as System.Drawing.Color = My.MySettings.Default.TheColor
dim theFont as System.Drawing.Font = My.MySettings.Default.TheColor

I have found in the SettingsProperty objects, the default value in a
serialized form :

dim theColorProperty as SettingsProperty =
My.MySettings.Default.Properties("TheColor")
dim theFontProperty as SettingsProperty =
My.MySettings.Default.Properties("TheFont")

dim defaultColorValue as String = theColorProperty .DefaultValue
dim defaultFontValue as String = theFontProperty .DefaultValue

Now, how can I build the Color and Font object containing the default values
from these SettingsProperty ? Is there a way to deserialize it ?

Thanks.
 
Pass the color to Color.FromName() which will give you a color back. Font has
a constructor that takes the name and size (empoints).
e.g

dim defColor as Color = Color.FromName(defaultColorValue)
dim defFont as new Font(defaultFontValue, 12)


Ciaran O'Donnell
 
Thanks for your answer.

I understand that you suggest me to write the deserialization code by
myself. I think I could do that but I was looking for a "standard" way to
deserialize these objects ( Colors, Fonts or anything else). There must be
something like this since application and user properties are serialized and
deserialized by the ApplicationSettingsBase object.

Is there another way to retrieve default values for user properties ?
 
Hi,

Try the following:

(Imports System.ComponentModel)

Dim property As SettingsProperty =
My.MySettings.Default.Properties("TheColor")

Dim converter As TypeConverter =
TypeDescriptor.GetConverter(property.PropertyType)

Dim instance As Object = converter.ConvertFromString(property.DefaultValue)

You have to cast property.DefaultValue to System.String, but I can't remember
how to do that in VB.NET - sorry
 
Hi,

That's exactly what I was trying to do. I've been looking for that for many
days.

Thank you so much for your help.
 
Back
Top