views:

148

answers:

2

I created a simple User Settings Dialog by binding the Property.Settings to a PropertyGrid.

This works like a charm but now I would like to allow only certain choices for some values. I have noticed that some Types will give a dropdown of possible choices. This is what I am shooting for but for, say, Strings.

Example, one of the Settings is UserTheme which is a String. Black, Blue, Silver. The program reads that string from the Settings File and sets the Theme on Startup.

I can type in a correct theme and it works but if I type in Pink it will not as there is not a pink option.


This is my VERY simple UserSettingsForm code.

    #region FIELDS

    internal Settings userSettings;

    #endregion

    #region EVENTS

    private void frmEditUserControl_Load(object sender, EventArgs e)
    {
        userSettings = Settings.Default;
        this.propertyGrid1.SelectedObject = userSettings;
        this.propertyGrid1.PropertySort = PropertySort.Alphabetical;
    }

    private void btnSave_Click(object sender, EventArgs e)
    {
        userSettings.Save();
        //this.DialogResult = DialogResult.OK;
        this.Close();
    }

    private void btnCancel_Click(object sender, EventArgs e)
    {
        userSettings.Reload();
        this.Close();
    }

    #endregion

EDIT

Okay, following the advice here I created a library file with my enum in it. Referenced the dll in my main app. Now in settings I see the enum but the dropdown only gives the first enum as an option. Ideas?

namespace psWinForms
{
    public enum UserTheme
    {
        Blue,
        Black,
        Silver,
        Green,
        Pink
    };
}
+2  A: 

The Visual studio Settings editor shows a drop down automatically for enumeration types. You can try to create a UserTheme enumeration and test that the PropertyGrid behaves the sames as the Visual Studio Settings editor.

public enum UserTheme
{
    Black,
    Blue,
    Silver
}

Update: I just tested and PropertyGrid automatically shows a drop down for an enumeration type.

João Angelo
Nice. I will give that a look. Any guesses on WHERE to create that `enumeration` so that it'll be picked up?
Refracted Paladin
@Refracted Paladin, IIRC when you choose the type of a setting you are allowed to browse into referenced assemblies. So placing your public enumeration in a library should work.
João Angelo
@Joao Angelo: So you are right, in, that Enums is the way to go. Unfortunately I cannot get it to show in the settings. See my edit for details.
Refracted Paladin
@Refracted Paladin, do not nest the enumeration type inside a class. I never tried that and don't know if the Settings designer supports that in any way.
João Angelo
A: 

What you need is a TypeConverter class. (System.ComponentModel) You can then associate a class with a Typeconverter via an attribute. (Even a property if I am not mistaken)

The methods you need to implement then are the GetStandardValues and related methods.

There is much documentation available on the net.

MaLio