VB.NET get default value from My.Settings
Asked Answered
C

3

5

VB2010. I understand how to save settings and load them from the My.Settings namespace. What I am trying to figure out is how to get the default value of a settings and present it to the user. I don't want to save the setting I just want to show what the current value is and what the default value is. I tried this but I don't think it does what I think it's supposed to do:

 Msgbox "Current setting: " & My.Settings.CustLineWidth & vbcrlf & _
        "Default setting: " & My.MySettings.Default.CustLineWidth

The default as I have setup in the IDE is 10 but the user changed it to 25. If I run the above code I get

 Current setting: 25
 Default setting: 25

What I would like is

 Current setting: 25
 Default setting: 10

Solution: I iterate through all the settings and print out the current value and the default value like this

        For Each prop As Configuration.SettingsPropertyValue In My.Settings.PropertyValues
            Debug.Print("Name={0}", prop.Name)
            Debug.Print("  Value  ={0}", prop.PropertyValue.ToString)
            Debug.Print("  Default={0}", prop.Property.DefaultValue.ToString)
        Next prop
Corporeal answered 8/11, 2013 at 21:46 Comment(0)
E
6

This worked for me:

My.Settings.PropertyValues("CustLineWidth").Property.DefaultValue
Estray answered 8/11, 2013 at 22:40 Comment(0)
C
1

Use This Code only :

MsgBox(CStr(My.Settings.Properties.Item("CustLineWidth").DefaultValue))

This code returns default value : with unhandled error

My.Settings.PropertyValues("CustLineWidth").Property.DefaultValue

But this one only returns last value before calling "settings.save" method : : with unhandled error

My.MySettings.Default.CustLineWidth
Calx answered 7/1, 2015 at 18:50 Comment(0)
V
0

Let's say that you have added this to your settings file, you just need to do something like below

My.Settings.YOUR_SETTING_NAME
Vagary answered 9/5, 2016 at 10:43 Comment(1)
Yes but lets say you have 100 settings, you don't want to do this for every setting. Much easier to iterate through the collection and get a string value of the variable name and then use it in My.Settings.PropertyValues("CustLineWidth").Property.DefaultValue. Ill post my approach as soon as I finish it.Corporeal

© 2022 - 2024 — McMap. All rights reserved.