开发者

Winform User Settings - Allow multiple choice values at Runtime

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

Th开发者_StackOverflow中文版is 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
    };
}


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜