I've got a main class which on user event shows the settings window. There are some textboxes, radiobuttons etc. After pressing the Save button, I want to store the data to some file.
I think that the saving process should be inside the main class and settings window only takes care of displaying the current config data, validate the new data and then send them back to the main class. Is this right?
How can I "send" the current config data from main class (which knows where the config file is etc.) to the window and then "send" the new data from window to main class to be stored?
Thanks
In WPF you can use Properties.Settings (Best solution for storing any application settings). See this.
Or you can create your own settings file, as an example, you may create xml file, and store your data into it.
See:
XmlSerializer
DataContractSerializer
Related
When should I really call the Reload method of the Properties.Settings.Default? Every time when I want to access properties from the persistent storage? Is it really necessary?
Thanks in advance.
The documentation for ApplicationSettingsBase.Reload states:
Refreshes the application settings property values from persistent storage.
So yes, you should call it whenever you want to access properties from the persistent storage. What you didn't ask was:
Do I need to load settings from persistent storage every time I want the value?
The answer to that is no. The value will be cached in the application settings object, so you do not need to reload if you just want the cached value.
The real answer for me is I can't think of a reason to call it at all. I'll tell you why:
There are two types of settings. User settings and application settings. The user settings are changed on a per-user basis and most probably through a UI that belongs to your application itself. If you change the settings within your application, it already knows they have been changed - reload unnecessary.
The application settings should not change after the application has been installed. How should they? Only admins have access to the program files folders at all.
Applications that run outside user space (services) have no GUI and there's no good reason to use user settings for services. Normally you'll change the config file and then restart the service.
The settings are loaded once when the application is started. Should you re-read them every time before you access a setting? No - for the reasons I gave above.
Here is an example of when to use Reload() method:
I have built an editor for those property settings. It is a propertygrid control with a few buttons on a dialog form.
When the user clicks to invoke that editor then key/values from Properties.Settings.Default are shown in the propertygrid.
Now, users can change many properties but if they decide NOT to save those changes, settings have to be reloaded because they are now invalid!
And that is when I use:
((ApplicationSettingsBase)propertyGrid.SelectedObject).Reload();
I want to save state of lot of CheckBoxes (checked/unchecked) and some TextBoxes on a WPF screen in .settings file (As I do not want to use database/filing).
Whether I have to save all these checkboxes and textboxes in setings file or there is a better way to do it?
If this is to remember the state of controls in an application, I would definitely use user settings to store this. Creating a separate setting for each control can be time consuming initially, but it makes life a little easier later. However, if you don't want to do this, you can simply create a custom settings class with a public property for each control and then simply use an instance of that class as your one savable setting.
I am creating a application using Composite UI Application Block(CAB).I have taken a TabWorkspace.
My problem is that I am taking a handle of a cam device in one tab and want to pass the handle in other tab.
Any one can suggest how to pass the value in one tab to other.
Make a common.cs file. you have to assign first in an bvariable in cs file and get it
So I've seen many questions that talk about the best method to store settings but haven't really seen any that deal with my situation.
Basically my app today has one settings tab with about 75 options/settings. A user is able to basically have different "copies" of these settings (think: dropdown that when you select it, all 75 options are set to whatever was in that associated .xml file).
Right now I just store settings using XML. The problem is that when my application needs to ACCESS a setting, it is literally accessing the UI directly (checkboxes, etc...)
My question: Should I basically created a "has changed" event for every control, and then update the appropriate setting structure (and only save when the user hits save)?
I'm worried that I shouldn't be accessing the UI directly, and it's doing that today ALL over the app. I want to be sure that my new solution is appropriate/how C# was designed.
Thanks in advance!
~ Josh
It sounds like you want to have a class that represents a configuration. Serializing to/from XML allows you to store it in a file or database.
A static singleton can provide the application with the currently active configuration's values. The application should not be checking the UI elements on a non-visible form to get the settings each time one is needed.
Designing the settings form so that it handles a instance of a configuration allows it to be used to display/update the active configuration or a saved configuration. It ought to simplify letting a user load a configuration, modify it, and save it as a new configuration.
And 75 is, as a rule, a lot of settings for a single tab to manage.
I have a windows application developed in c sharp with setup file generated. For that file I need to save the form with the controls and text with in controls. This application don't have any database. Can you suggest an extension method and also how to save the form?
There is no way to save controls. What you can do is create a serializable class to keep Form's data. You can serialize those settings and save. When you load the application next time you can deserialize and load those values to the form controls. This is one of the ways of doing it if you don't use a database to store data.