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:

    _
    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.

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


Comments (17):

  1. Seth says:

    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. 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 says:

    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 Gottlieb Svendsen Jakob says:

    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 says:

    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 :D

  6. Yaiza says:

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

  7. Ignacio Soler says:

    Thanks a lot. A very valuable information.

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

  8. Michael L. Long says:

    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 says:

    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 says:

    @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. Alex says:

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

  12. sadheer says:

    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

  13. jpp says:

    Grate post.
    Many many thanks!

  14. Greg says:

    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?

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

Leave a Reply

CAPTCHA Image
(required)