C #: Where do you name your own save method after clicking OK in the settings dialog for your program?

Where would you place the SaveSettings method in your project when the user edited any parameters in the settings dialog box in your program?

Should it be in return like:

using (frmSettings frmSettings = new frmSettings()) { if (frmSettings.ShowDialog() == DialogResult.OK) { // clicked OK, should I call SaveSettings() here? } else { // clicked cancel. } } 

Or where to place it?

+4
source share
5 answers
 // clicked OK, should I call SaveSettings() here? 

It looks like a good place. =)

EDIT: I suppose it depends on the structure of the application, but there is nothing wrong with putting it there. This is a logical (by all definitions of logic) place to put it.

+3
source

Putting the save code in the calling form, in my opinion, puts it in the wrong place. Yes, it will work in this case, but this means that the settings form cannot be reused and that any error in your save code will disable the settings form before you know about any errors.

In addition, if you add a new parameter, you need to make changes to two sources, add controls (and initialize them) once in the settings form and save the values ​​in the calling form once.

I would bind the code to the OK button of the settings form. If any errors occur during saving, you can inform the user while their changes are visible and can be restored. A form can be called from different places as needed, or moved only by moving the ShowDialog () call. Your DialogResult.OK processing should be used to update the call form, as changes apply to it.

+4
source

It depends on what the form settings do. If he just starts the user or cancels it, then

 // clicked OK, should I call SaveSettings() here? 

- a good place.

However, if you get the configuration information in a dialog form, I would put the save logic in this form.

+1
source

I definitely don't think there is anything logically wrong with setting the save function that you suggested. As an alternative, at least in some cases, I believe that it would be more appropriate to call a function to save the settings in the "Settings" form; but it depends a lot on the overall architecture of your application.

0
source

If the frmSettings form contains the parameters that you need to save, I would save the settings from this form - either in isolated storage or in an external XML file depending on your design.

0
source

All Articles