Winform 用户设置 - 允许在运行时选择多个值
我通过将 Property.Settings
绑定到 PropertyGrid
创建了一个简单的用户设置对话框。
这就像一个魅力,但现在我只想允许某些值的某些选择。我注意到某些类型会提供可能选择的下拉列表。这就是我拍摄的目的,但是,比如说,弦乐。
例如,设置之一是UserTheme,它是一个字符串。 黑色、蓝色、银色。该程序从设置文件中读取该字符串并在启动时设置主题。
我可以输入正确的主题并且它可以工作,但如果我输入粉红色,它就不会,因为没有粉红色选项。
这是我非常简单的 UserSettingsForm 代码。
#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
编辑
好的,按照这里的建议,我创建了一个库文件,其中包含我的enum
。在我的主应用程序中引用了dll
。现在,在设置
中,我看到了枚举
,但下拉列表
仅提供第一个枚举
作为选项。有想法吗?
namespace psWinForms
{
public enum UserTheme
{
Blue,
Black,
Silver,
Green,
Pink
};
}
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
};
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Visual Studio 设置编辑器自动显示枚举类型的下拉菜单。您可以尝试创建 UserTheme 枚举并测试 PropertyGrid 的行为是否与 Visual Studio 设置编辑器相同。
更新:我刚刚测试过,
PropertyGrid
自动显示枚举类型的下拉菜单。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.
Update: I just tested and
PropertyGrid
automatically shows a drop down for an enumeration type.您需要的是一个 TypeConverter 类。 (系统.组件模型)
然后,您可以通过属性将类与类型转换器关联起来。 (如果我没有记错的话,甚至是一个属性)
您需要实现的方法是 GetStandardValues 和相关方法。
网上有很多可用的文档。
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.