Update Settings in Winows Forms project
-
I haven't done a c# project in some time, and trying to remember all the things I've forgotten. I have an application with various application & user settings. I want the user to be able to set those preferences. I figured out how to use a property grid to show settings on the appropriate form:
Properties.Settings mySettings = new Properties.Settings(); private void SettingsForm\_Load(object sender, EventArgs e) { this.propertyGrid1.SelectedObject = mySettings; }
It seems I can't bind the Settings directly to the property grid, I had to create an object from the settings and bind to that. I put the object assignment at form level in case I needed it to update later, but it could also be assigned in the form load event if I don't need to refer to it later. This shows application settings as disabled and user settings enabled in the property grid, as expected. Now I want to change a setting and save that back to the actual settings file. I can't seem to find any recent article on how to do that. I found an article from 2009 but it was quite complex, and I feel like that should be simpler in .Net 5. Did I properly load the property grid? Is there a way to update all changed user settings in 1 go, or do I need to iterate each property grid item, verify if it was changed, then update the actual setting? How do I iterate the grid items, skipping categories & disabled items, and put them back into the settings? Thanks so much for any help....
-
I haven't done a c# project in some time, and trying to remember all the things I've forgotten. I have an application with various application & user settings. I want the user to be able to set those preferences. I figured out how to use a property grid to show settings on the appropriate form:
Properties.Settings mySettings = new Properties.Settings(); private void SettingsForm\_Load(object sender, EventArgs e) { this.propertyGrid1.SelectedObject = mySettings; }
It seems I can't bind the Settings directly to the property grid, I had to create an object from the settings and bind to that. I put the object assignment at form level in case I needed it to update later, but it could also be assigned in the form load event if I don't need to refer to it later. This shows application settings as disabled and user settings enabled in the property grid, as expected. Now I want to change a setting and save that back to the actual settings file. I can't seem to find any recent article on how to do that. I found an article from 2009 but it was quite complex, and I feel like that should be simpler in .Net 5. Did I properly load the property grid? Is there a way to update all changed user settings in 1 go, or do I need to iterate each property grid item, verify if it was changed, then update the actual setting? How do I iterate the grid items, skipping categories & disabled items, and put them back into the settings? Thanks so much for any help....
Quote:
The topics in this section describe how to use settings at design time and run time.
"Before entering on an understanding, I have meditated for a long time, and have foreseen what might happen. It is not genius which reveals to me suddenly, secretly, what I have to say or to do in a circumstance unexpected by other people; it is reflection, it is meditation." - Napoleon I
-
Quote:
The topics in this section describe how to use settings at design time and run time.
"Before entering on an understanding, I have meditated for a long time, and have foreseen what might happen. It is not genius which reveals to me suddenly, secretly, what I have to say or to do in a circumstance unexpected by other people; it is reflection, it is meditation." - Napoleon I
Thanks Gerry. I had already read that article, but I'm still not understanding how to get the (maybe) changed settings out of the Property Grid and back into the actual Settings object before saving the settings to the proper settings file. I'm thinking I need to iterate each setting in the Property Grid, compare it in code to the Settings object, and for each one that doesn't agree, update that setting in code, THEN execute the save command. Remember, I instantiated the Settings object, and used that to bind to the Property Grid. Still not sure I did that part correctly either, but it does cause the settings to appear in the Property Grid.
-
Thanks Gerry. I had already read that article, but I'm still not understanding how to get the (maybe) changed settings out of the Property Grid and back into the actual Settings object before saving the settings to the proper settings file. I'm thinking I need to iterate each setting in the Property Grid, compare it in code to the Settings object, and for each one that doesn't agree, update that setting in code, THEN execute the save command. Remember, I instantiated the Settings object, and used that to bind to the Property Grid. Still not sure I did that part correctly either, but it does cause the settings to appear in the Property Grid.
Never used it but the docs say the Property Grid is for "browsing" ... How are you actually changing the settings? I have my own custom "settings" code (serialize; deserialize). I verify the settings as a whole; I don't need to go back and see what "changed". Any object can be iterated using reflection; so if you had to compare, I'd use reflection to compare like named members if it came down to it.
"Before entering on an understanding, I have meditated for a long time, and have foreseen what might happen. It is not genius which reveals to me suddenly, secretly, what I have to say or to do in a circumstance unexpected by other people; it is reflection, it is meditation." - Napoleon I
-
I haven't done a c# project in some time, and trying to remember all the things I've forgotten. I have an application with various application & user settings. I want the user to be able to set those preferences. I figured out how to use a property grid to show settings on the appropriate form:
Properties.Settings mySettings = new Properties.Settings(); private void SettingsForm\_Load(object sender, EventArgs e) { this.propertyGrid1.SelectedObject = mySettings; }
It seems I can't bind the Settings directly to the property grid, I had to create an object from the settings and bind to that. I put the object assignment at form level in case I needed it to update later, but it could also be assigned in the form load event if I don't need to refer to it later. This shows application settings as disabled and user settings enabled in the property grid, as expected. Now I want to change a setting and save that back to the actual settings file. I can't seem to find any recent article on how to do that. I found an article from 2009 but it was quite complex, and I feel like that should be simpler in .Net 5. Did I properly load the property grid? Is there a way to update all changed user settings in 1 go, or do I need to iterate each property grid item, verify if it was changed, then update the actual setting? How do I iterate the grid items, skipping categories & disabled items, and put them back into the settings? Thanks so much for any help....
I got this working now. It loads the settings into the PropertyGrid perfectly. Since I'm not limiting it to User settings, it shows them all, both User & Application. Since Application settings are read-only, the PropertyGrid helpfully makes those grey'd out. Interesting quirk: If I bind the PropertyGrid to an object instantiated from the settings themselves, changing a setting in the PropertyGrid directly updates the User settings. Just need to remember to save the settings before closing the form. Since I wanted this to not auto-change the settings, I created a Save button, and bound the form to a settings-type object using the New keyword. If I change a setting in the PropertyGrid, I iterate its items and compare each value to the current matching setting value. If different, I update the settings right then, and set a bool flag that something has changed. After completing iteration I save the settings and we're done. Works great. Now I want to be able to add a new setting. Does anybody know what the enumeration name is that contains the different setting types? You can see them in the project properties, Settings tab, when you go to add a setting. The Type drop-down shows many types (string, char, bool, int,...). I'd love to be able to choose which type in code when trying to add a new setting. Thanks...
-
Never used it but the docs say the Property Grid is for "browsing" ... How are you actually changing the settings? I have my own custom "settings" code (serialize; deserialize). I verify the settings as a whole; I don't need to go back and see what "changed". Any object can be iterated using reflection; so if you had to compare, I'd use reflection to compare like named members if it came down to it.
"Before entering on an understanding, I have meditated for a long time, and have foreseen what might happen. It is not genius which reveals to me suddenly, secretly, what I have to say or to do in a circumstance unexpected by other people; it is reflection, it is meditation." - Napoleon I
Thanks Gerry. Not sure what is meant by "browsing". If the property grid is bound to the Properties.Settings collection, they (the settings) just appear in the property grid at run-time. Depending on how the grid is bound, it either updates the settings immediately if you make a change, or you have to iterate the grid items to see if any changes were made. For each of the changes, you need to update the setting itself. This is decided when instantiating an object to the settings collection before binding the property grid to it. If you use the New keyword, settings are not auto-updated, you need to update each grid item value to settings. If not using New, the changed values are updated immediately. Either way, you need to remember to save the settings. I chose the first method (New) so if a user "accidentally" changes a setting in the grid, they just click the Cancel button and nothing is changed. Otherwise, they need to click the Save button.
-
I got this working now. It loads the settings into the PropertyGrid perfectly. Since I'm not limiting it to User settings, it shows them all, both User & Application. Since Application settings are read-only, the PropertyGrid helpfully makes those grey'd out. Interesting quirk: If I bind the PropertyGrid to an object instantiated from the settings themselves, changing a setting in the PropertyGrid directly updates the User settings. Just need to remember to save the settings before closing the form. Since I wanted this to not auto-change the settings, I created a Save button, and bound the form to a settings-type object using the New keyword. If I change a setting in the PropertyGrid, I iterate its items and compare each value to the current matching setting value. If different, I update the settings right then, and set a bool flag that something has changed. After completing iteration I save the settings and we're done. Works great. Now I want to be able to add a new setting. Does anybody know what the enumeration name is that contains the different setting types? You can see them in the project properties, Settings tab, when you go to add a setting. The Type drop-down shows many types (string, char, bool, int,...). I'd love to be able to choose which type in code when trying to add a new setting. Thanks...
This one's on how to create new application settings. [How to: Create Application Settings - Windows Forms .NET Framework | Microsoft Learn](https://learn.microsoft.com/en-us/dotnet/desktop/winforms/advanced/how-to-create-application-settings?view=netframeworkdesktop-4.8)
"Before entering on an understanding, I have meditated for a long time, and have foreseen what might happen. It is not genius which reveals to me suddenly, secretly, what I have to say or to do in a circumstance unexpected by other people; it is reflection, it is meditation." - Napoleon I