Runtime AppSettings.settings editor dialog

2019-08-21 17:04发布

Essentially I'm looking for this type of project: http://www.codeproject.com/Articles/6160/Application-Configuration-Editor-using-the-Propert

But for the Application Settings. Have you heard of it, could you advice anything?

Basically the usecase is that once set, AppSettings could be changed only from the code and if you want to change anything by hand, you've got very limited and complex options.

The idea is to allow user to run the app with '--config' parameter and to allow him to change any setting there is in the interface that resembles Visual Studio's own editor:

enter image description here

Edit: of course, in runtime the only editable column should be Value. And the only other visible one should be Name, like here:

enter image description here

1条回答
家丑人穷心不美
2楼-- · 2019-08-21 17:27

Oh, haven't thought of it, just put your ApplicationSettingsBase into PropertyGrid.SelectedObject and you can edit it. Then you just call .Save() and your settings are saved.

Simple as that.

Edit: If you're building such an editor, consider adding the button that resets settings to their default values with the .Reset() method. By defaults I mean values that you specified in designer editor in Visual Studio.

With this button there're two main gotcha's:

First, after .Reset() is called, new (default) settings are already saved to disk, so there will be no obvious way of recovering them (if any at all). So, please, prepare your user for that.

Secondly, you'll need to call .Refresh() on your PropertyGrid, because it wouldn't refresh its fields (but when you'll set cursor to the field, it will refresh, so data is not saved, it's just still painted on the control).

Another edit: To make the process of editing settings even more user-friendly, you would probably want to organize them in groups, change names to more readable and such.

Unfortunately code for the settings object is autogenerated, which means that you can't add any attributes to the properties in that class. Fortunately, you can add these properties programmatically.

查看更多
登录 后发表回答