During the work of a new Coretech SCCM Manager GUI in WPF, i ran in to some problems with My.Settings.
I have used My.Settings for all settings in the Utility, and are using my own structures for the combo-box value and others.
The problem is that i could not create a setting, that contained my own structure instead of a system data type.
I simply could not select it in the list:
I tried to use the browse function, but no luck.
I have searched the net for ages, trying to find a solution, but never found one.
This problem can occur in both Windows Forms And Windows Presentation Foundation (WPF) projects
After while i figured out how to do it:
1. Open the Settings.Designer.vb file
2. If you are working in an WPF project, you cannot select the code file behind the GUI for the settings. Therefore you have to go to the Project folder and open the file itself in \ProjectFolder\My Project\
3. Now copy one of the properties already in there:
_
Public Property OSDEnabled() As String
Get
Return CType(Me("OSDEnabled"),String)
End Get
Set
Me("OSDEnabled") = value
End Set
End Property
4. Change the new copy to your custom datatype, in my case it was my “Definitions” structure class
_
Public Property OSDCollection() As Definitions
Get
Return CType(Me("OSDCollection"), Definitions)
End Get
Set(ByVal value As Definitions)
Me("OSDCollection") = Value
End Set
End Property
5. Now it should be working, but the GUI in Visual Studio still does not show the new data type, but luckily this can be fixed!
6. Open Settings.Settings file in the same folder
7. Copy one of the old settings and change it to the new data type (just like you did before).
Copy one of the other settings:
Change it to your new type:
8. Now everything should be working and you data type has appeared in the dropdown list in Visual Studio.
I hope this small article is helpful.
P.S. I was working in Visual Studio 2008, but i think it is possible to do the same in 2005.
– Jakob
Jakob,
Thank you for the article. Is there anything special that needs to be done in the class/type I am creating? I have followed your instructions but the data type does not show up for me. The only step I could not follow was the last step where I change something in the Settings.settings file. I do not see the same thing you see.
Hello Seth.
I am sorry, but this post was curreupted by an update.
that is why you did not see the same.
i have updated the post now, i hope it helps.
– Jakob
hello Seth.
What if i am using c#. what i have to do? Because its been long i am trying to do this.
-Fatima
Hello Fatima
You can do almost the same in C#.
It is the same files (they are called .cs instead of .vb).
The syntax is ofcause a little bit different, but the method is exactly the same.
Copy an existing settings to a new one in Settings.Designer.cs
and do the same in Settings.Settings.
Remember to open settings.settings in a text editor, instead of the internal settigns designer. (Use Open With.. in VS)
I hope this helps
– Jakob
just fyi, you can simply modify the settings.settings file with the xml editor built into visual studio, and the changes will reflect accordingly in settings.designer, without you having to edit both files 😀
Thank you very much!!! I was struggled with the very same problem and it was great to find a solution.
Thanks a lot. A very valuable information.
I just can not understand why the Browse function does not work.
Excellent post thank you. It would seem to me Microsoft could add functionality to its designer to parse project types that are serializable / provide their own type converters, since those are the 2 main requirements (either or) as far as I know for a serializable property in settings. But that is simply standard to have a class serialize anyway so nothing new there, it’s just not automated. Maybe in a future build?
Anyway, thanks again. Simple and elegant solution and opens up the settings to be a lot more robust now.
Actualy if the browse does not work, you can just write the class name including namespace into the text box in the browse dialog. It worked for me.
What didn’t work is the serialization of this selected class. No matter that it was marked as serializable.
@polygon
I’m having exatly your problem!
——
[Serializable]
public class WebCamProperties
{
protected string _source;
public string source
{
get { return _source; }
}
protected Size _framseSize;
public Size framseSize
{
get { return _framseSize; }
}
public WebCamProperties()
{
}
public WebCamProperties(string src, Size frame)
{
this._source = src;
this._framseSize = frame;
}
}
—–
If I try to “settings.Save()” the file will not writed, so I’think that the system is unable to serialize my object and then will not write it correctly.
Any suggestion?
have a look at this article:
http://stackoverflow.com/questions/1321248/how-do-i-save-serialize-a-custom-class-to-the-settings-file
Seems like this may be a better solution.
[…] http://blog.coretech.dk/scripting/add-custom-data-type-structure-to-mysettings-in-vbnet-wpf/ […]
Hey, I’m looking to store a list of my custom objects to the setting, how would I go about doing that?
please help me i want to create new user with out any sql data storage i want to create new user using the application setting in vb.net plz for God help me on this mail
[email protected]
Grate post.
Many many thanks!
This doesn’t seem to work for me in VS2010/C#.
Every time I re-open the settings.settings.cs file, my “set” accessor is removed by the code generator.
any idea’s?
very fine info i am very pleased to see your post.
Hi,
The editing of the Settings.settings file is missing from the post as it is XML this is an example of how I corrected mine:
from this:
<Setting Name=”FeedbackLogErrorLevel” Type=”System.String” Scope=”Application”>
<Value Profile=”(Default)”>WARNING</Value&
</Setting>
to this:
<Setting Name=”FeedbackLogErrorLevel” Type=”Diagnostic.Logger.ErrorLevelType” Scope=”Application”>
<Value Profile=”(Default)”>WARNING</Value&
</Setting>
Hope this helps others, this post really helped me, I can’t see why you can’t brows types in your own project, it is obvious you may need them in settings!
Guy
PS: I used json to store complex objects that I needed in settings too, simple to serialize and deserialize them using:
Imports System.Web.Script.Serialization
Public Sub LoadStructureDefinitions()
Dim js As New System.Web.Script.Serialization.JavaScriptSerializer
Dim jsonString As String = My.Settings.MySetting
_DirectoryDefinitions = js.Deserialize(Of MySetting)(jsonString)
End Sub
excuse the VB but my current project has to be in VB!