Add Custom Data Type / Structure to My.Settings in VB.NET / WPF

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:

image

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:

image

1. Open the Settings.Designer.vb file

image

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:

4. Change the new copy to your custom datatype, in my case it was my “Definitions” structure class

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.

image

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

By | 2009-03-05T16:15:14+00:00 March 5th, 2009|Scripting & Development|19 Comments

About the Author:

Jakob Gottlieb Svendsen

Twitter: @JakobGSvendsen

Jakob Gottlieb Svendsen is a Microsoft Cloud and Data Center Management MVP (http://mvp.microsoft.com/en-us/default.aspx), Working as Global Lead Developer, Senior Consultant and Trainer at CTGlobal, where he is one of the driving forces in keeping CTGlobal a System Center Gold Partner and member of the System Center Alliance.

Since he started at Coretech in 2007, he has focused on Scripting and Development, primarily developing tools, extensions and scripts for the System Center Suite. His main area is Automation (including OMS/Azure Automation, Service Management Automation, PowerShell and Orchestrator). Another area is Windows Azure Pack / Azure Stack, where he does implementation, development, workshops and presentations. He is a world-wide renowned voice in the Automation field.

He is passionately devoted to the community, to which he contributes by being a moderator at TechNet and sharing his knowledge at http://blog.ctglobalservices.com/jgs

  • Co-founder: PowerShell User Group Denmark
  • Speaker at MMS 2016, Minneapolis (www.mmsmoa.com)
  • SCU Europe 2014, 2015, 2016 (www.systemcenteruniverse.ch)
  • Microsoft TechEd North America 2014, Houston
  • NIC 2012,2013,2014,2015, Oslo (www.nic.com)
  • Microsoft CampusDays 2011, 2013, Copenhagen
  • Microsoft TechDays 2015, Sweden (www.techdays.se)
  • Microsoft Partner Event: New in SC2012 SP1
  • User group meetings (PSUG.DK , SCUG.DK/BE/NO, AZMUG + more)
  • Microsoft Certified Trainer.
  • Microsoft Scripting Guys Forum Moderator

Main working areas:

  • Automation (Azure Automation, SMA, SCO)
  • Windows Azure Pack / Azure Stack
  • System CenterVisual Studio Team Services / Team Foundation Server
  • Development:C#.Net, VB.NET, VBScript, PowerShell, Service Manager, OpsMgr, ConfigMgr
  • Orchestrator
  • Windows Azure Pack / Azure Stack

Training:

  • Azure Automation
  • Service Management Automation
  • System Center Orchestrator
  • PowerShell, VBScript, C#.Net, VB.Net
  • Windows Azure Pack / Azure Stack Development Workshops

19 Comments

  1. Seth April 27, 2009 at 16:04 - Reply

    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.

  2. Jakob Gottlieb Svendsen
    Jakob Gottlieb Svendsen April 28, 2009 at 9:15 - Reply

    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

  3. Fatima August 3, 2009 at 11:45 - Reply

    hello Seth.
    What if i am using c#. what i have to do? Because its been long i am trying to do this.
    -Fatima

  4. Jakob August 3, 2009 at 11:54 - Reply

    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

  5. Anon-E-Moose October 23, 2009 at 6:32 - Reply

    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 😀

  6. Yaiza December 4, 2009 at 19:06 - Reply

    Thank you very much!!! I was struggled with the very same problem and it was great to find a solution.

  7. Ignacio Soler December 15, 2009 at 19:07 - Reply

    Thanks a lot. A very valuable information.

    I just can not understand why the Browse function does not work.

  8. Michael L. Long January 31, 2010 at 20:10 - Reply

    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.

  9. polygon February 3, 2010 at 16:19 - Reply

    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.

  10. chim March 24, 2010 at 1:07 - Reply

    @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?

  11. Bob May 18, 2010 at 16:10 - Reply

    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.

  12. Alex February 12, 2011 at 23:31 - Reply

    Hey, I’m looking to store a list of my custom objects to the setting, how would I go about doing that?

  13. sadheer February 15, 2011 at 7:43 - Reply

    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
    sadheerdanish@hotmail.com

  14. jpp February 3, 2012 at 22:19 - Reply

    Grate post.
    Many many thanks!

  15. Greg March 13, 2012 at 13:57 - Reply

    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?

  16. Firmajulegaver September 18, 2014 at 3:08 - Reply

    very fine info i am very pleased to see your post.

  17. Guy September 2, 2016 at 8:39 - Reply

    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

  18. Guy September 2, 2016 at 8:45 - Reply

    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!

Leave A Comment