35

I am trying to set the type of an application setting property to a custom enum type I have defined in my assembly (call this Project A)

In the settings browser I click browse and am presented with the "Select a Type" dialog box.

And the Types defined in Project A are not seem to be available to me. Yet types are available from other projects that A has referenced.

It seems almost beyond comprehension to me that one would not be able to use Types defined in the base project, so I assume I am doing something wrong. I have tried building, cleaning, rebuilding, restarting without any luck, so what exactly am I missing?

edit: screenshot here http://tinypic.com/r/2ls8myb/7

fostandy
  • 4,282
  • 4
  • 37
  • 41
  • Possible duplicate of [Using own enum in settings](http://stackoverflow.com/questions/11916540/using-own-enum-in-settings) – Legolas Feb 22 '16 at 15:27
  • 7
    for what it's worth, I asked this question in October 2010 and the [using own enum in settings](http://stackoverflow.com/questions/11916540/using-own-enum-in-settings) was asked afterwards in August 2011 so it was difficult for me to find this question at the time. – fostandy Mar 23 '16 at 23:40

4 Answers4

23

I was trying to do exactly the same thing in VS 2010 (.net 3.5). It turns out that you can do this using the Settings designer. The UI is totally flawed, as stated, but if you manually enter the fully qualified name of the enum in the Browse window it'll work. This has the benefit of not modifying generated code (*.Designer.cs), which is always good to avoid because when you do there's no guarantee that you're changes won't be overwritten.

PS - This only seems to work if the enum is public.

Settings GUI:

enter image description here

Then manually type fully-qualified type name:

enter image description here

At one point I swear I saw a drop-down of the enum values in the Value column, but I haven't seen it since and you just have to type an appropriate value in.

Sean
  • 8,407
  • 3
  • 31
  • 33
  • 3
    Just in case this doesn't work for anyone, try making sure that the custom type is public. – Elliot Dec 25 '14 at 00:27
  • Just to confirm: I also saw the dropdown you mention, but it is not showing anymore... Awesome how trivial things can be very hard in WPF :( – heltonbiker Jan 27 '15 at 18:27
  • I feel this method is simpler and quicker than trying to edit the Settings.settings file by hand. – Stealth Rabbi Jan 24 '17 at 20:56
7

I could not browse to my custom type either. I was able to add my custom type by opening the Settings.settings file in a text editor and adding the setting to the Settings section like this:

<Setting Name="Options" Type="MyProject.MyOptions" Scope="User">
  <Value Profile="(Default)" />
</Setting>

The settings designer complains when I open it, but I just respond 'No' to that dialog, and I am able to use the custom setting in my project.

George H.
  • 71
  • 1
  • 1
  • This worked for me, after for some reason I can't comprehend @Sean's solution didn't work. – Tsahi Asher Jan 11 '17 at 10:38
  • 1
    Well, it almost worked. However, VS did not serialize the setting to the app.config file at all, which means it can't be set at runtime. – Tsahi Asher Jan 11 '17 at 12:11
2

Because your own type is not serializable. Make it serializable and you can define it in your settings.

Aykut Çevik
  • 2,060
  • 3
  • 20
  • 27
  • 12
    Hi. I marked the type as serializable. After a clean and rebuild it was still not visible. Can I ask why other types from other assemblies which are not marked as serializable are visible in the Type Browser? – fostandy Oct 28 '10 at 11:55
1

I was trying the same and the solution was very simple: Build first.

I added an enum class, defined some values and then tried to replace an int-value in the Settings.settings file with my new enum, so I don't have to match the value later. The type was not found. Because I did not build yet.

After Building once the new enum type is known and can be used :)

ecth
  • 1,215
  • 1
  • 16
  • 33