I have a ListBox on my Form, I want to save it and load the values when I start the application again.
How can I save a list on PrjName.Properties.Settings.Default
?
I have a ListBox on my Form, I want to save it and load the values when I start the application again.
How can I save a list on PrjName.Properties.Settings.Default
?
I found out that I can't directly save a List<string>
on the application settings, but I saw that I can save a StringCollection
.
And here I found out that it's very simple to convert from a StringCollection
to a List<string>
var list = stringCollection.Cast<string>().ToList();
No problem at all! Create a new setting, e.g. "MyListOfStrings", type doesn't matter.
then open settings file in a xml editor
your file will look like this:
now change it as shown below and save it
well, that's all, now it will look like that:
and in code:
MyListOfStrings
is defined this way, can it be accessed with the editor in the Visual Studio IDE? –
Stylize the property could not be created from it's default value, there is an error in XML document (1,1)
exception. What might be the problem? I've tried to set default values as strings (Red, Blue...) and using hexadecimal too (#FFFF00FF...) with no success. –
Amoral <?xml version="1.0" encoding="utf-16"?> <ArrayOfString xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <string1> <string2> </ArrayOfString>
–
Bennet .Save()
method will only work if you modify the list itself (say .Add
or .Remove
), if you want to save after you modify the content of an object already on the list, just reload the list with itself like so: Properties.Settings.Default.List = Properties.Settings.Default.List;
before calling .Save()
and it will work properly. This has to do with how the settings class handles detecting changes to avoid writing the same settings to the disk over and over. –
Hyperactive List<String>
), but then when it regenerates the code file, it takes it verbatim (in C#), so you CAN correct it to List(Of String)
for VB's purposes, but then you can't mess with the settings file or it'll overwrite. Unless there's another workaround for that particular problem? My team supports some legacy apps in VB.NET: "just use C#" is not an acceptable answer for legacy support; we use C# for new apps and this is an excellent tutorial answer for C#. –
Henrik I found out that I can't directly save a List<string>
on the application settings, but I saw that I can save a StringCollection
.
And here I found out that it's very simple to convert from a StringCollection
to a List<string>
var list = stringCollection.Cast<string>().ToList();
Dim list = stringCollection.Cast(Of String)().ToList()
; I've got legacy apps to support, and this is very helpful / elegantly simple. I used ToArray
with String.Join
, but same difference. –
Henrik I know this is an older question, but I feel it is noteworthy to add that you can create a custom class that inherits whatever kind of list you want to use and it makes it very simple to set. For example, if you want a List<CustomData>
, you could create the class like this:
using System.Collections.Generic;
namespace YourNamespace
{
public class CustomCollection : List<CustomData>
{
}
}
then open the settings like @pr0gg3r suggested and add this in the <Settings>
section:
<Setting Name="SomeSetting" Type="YourNamespace.CustomCollection" Scope="User">
<Value Profile="(Default)" />
</Setting>
You won't be able to use the settings designer, unless it's a basic data type like string or int. If you are using a custom data type, you'll have to initialize it on startup before using it, but I find it still looks more elegant than trying to directly use a list in the settings.
When using the natively supported Type System.Collections.Specialized.StringCollection
I used this code:
System.Collections.Specialized.StringCollection SavedSearchTerms = new System.Collections.Specialized.StringCollection();
if (Properties.Settings.Default.SavedSearches != null)
{
SavedSearchTerms = Properties.Settings.Default.SavedSearches;
}
SavedSearchTerms.Add("Any Value");
Properties.Settings.Default.SavedSearches = SavedSearchTerms;
© 2022 - 2024 — McMap. All rights reserved.
Dim list = stringCollection.Cast(Of String)().ToList()
; I've got legacy apps to support, and this is very helpful / elegantly simple. I usedToArray
withString.Join
, but same difference. – Henrik