How to save / restore a form and manage between program runs?

I have a complex form that allows the user to customize my application.

What is the best way to save the form state and reload with the next program.

I mean the text that he entered into the lists, the selected combo / list / radio element, is there a checkbox, etc.

+6
visual-studio-2008 winforms
source share
7 answers

Many people tell me when to save, but not many tell me how ...

In the end I went with WritePrivateProfileString ()

+3
source share

You have several options for saving the entered settings - in the configuration file or in the registry, possibly in the database (maybe even in the "cloud", but I won’t go there).

Before saving the settings, the user must perform a certain action (for example, click the "Apply" button) - you should not just save the settings when the user closes the form, as this is ultimately not a very good UX.

How you save the settings is completely up to you - you can save them to a style configuration file with a direct name / value, you can use XML in the configuration file or save them as keys and values ​​to a known place in the registry (or you can save name pairs / value in the database table).

When your application starts next time, one of the launch tasks may be to check a known location (whether it is a registry or a configuration file) for parameters, and then load them into the settings class. Make sure you have logical defaults for each parameter, if it has never been set or for some reason you cannot read it. The settings class can then be transferred to each form so that it applies any settings or it can be a static class (globally visible class of one instance) so that it can be easily read from anywhere in the application.

Edit: after reading your comment on another answer, here is another option, a bit more advanced. Use the settings class mentioned earlier, but also use the binding - you can bind your settings object directly to your form, so any entered values ​​will be updated directly in the settings object without the need to write code for this (provided that you use two methods of binding) . Streaming can be achieved by serializing the settings object to a file (or database), I suggest you look at the XmlSerializer .

+1
source share

Serialize the form.

ISerializable , and in the constructor of serializable and GetObject (), load / save your fields.

In OnClosing serialize the form.

  /// /// try to obtain the las serialized main form with old data MainForm mainForm = DeserializeMainForm("mainForm.data"); /// /// if any old data found, create a new(empty) main form if (mainForm == null) mainForm = new MainForm(); static MainForm DeserializeMainForm(string filePath) { MainForm mf = null; FileStream fileStream = null; try { BinaryFormatter binaryFormatter = new BinaryFormatter(); fileStream = new FileStream(filePath, FileMode.Open); mf = (MainForm)binaryFormatter.Deserialize(fileStream); } catch { } finally { if (fileStream != null) { fileStream.Close(); } } return mf; } 

MainForm:

 [Serializable] public partial class MainForm : Form, ISerializable { protected MainForm(SerializationInfo info, StreamingContext context) : this() { if (info == null) throw new System.ArgumentNullException("info"); this.tbxServerIp.Text = info.GetString("server ip"); this.tbxServerPort.Text = info.GetString("server port"); this.tbxEventFilter.Text = info.GetString("event filter"); this.tbxWallId.Text = info.GetString("wallId"); foreach (Control control in this.Controls) { if (control is EventSender) { EventSender eventSender = (control as EventSender); eventSender.LoadFromSerializationInfo(info); } } } private void SerializeThis() { BinaryFormatter binaryFormatter = new BinaryFormatter(); FileStream fileStream = new FileStream("mainForm.data", FileMode.Create); try { binaryFormatter.Serialize(fileStream, this); } catch { throw; } finally { fileStream.Close(); } } protected override void OnClosing(CancelEventArgs e) { SerializeThis(); base.OnClosing(e); } } 
+1
source share
 Private Sub frm_Closing (sender as Object, e as CancelEventArgs) Handles MyBase.Closing ' save all the values you want' End Sub Private Sub frm_Load(sender as Object, e as EventArgs) Handles MyBase.Load If SaveSettingsExist Then ' restore all the values you want' End If End Sub 
0
source share

In fact, I have several common routines that I use to save the settings for the form column / position and ListView columns. So I have something like ...

 Private Sub frm_Closing (sender as Object, e as CancelEventArgs) Handles MyBase.Closing SaveFormPos(Me) SaveListview(Me, lvuInvoices) End Sub Private Sub frm_Load(sender as Object, e as EventArgs) Handles MyBase.Load RestoreFormPos(Me) RestoreListview(Me, lvuInvoices) End Sub 

The Me parameter (for the Listview procedure) is used to create a key for the values ​​that will be stored in the registry. You have all kinds of options. You can put this functionality in the base class for all your Forms, create a SaveState class, or simply insert procedures into the module. You can save this data in the registry, in the database, in text files. You may have a general procedure that goes through the Controls collection that looks for TextBoxes, Checkboxes, etc.

However, once you have created a useful set of save procedures, you can use them in any subsequent form you want, so you only need to do the hard work once.

0
source share

I also agree with the set of functions LoadSettings/SaveSettings that is called when the form is created / when the application is closed.

As a storage location for application settings, I recommend using isolated storage .

In addition, depending on the controls you use in your form, you may be able to save your status in XML format and then restore it next time. For example, Infragistics allows you to use this feature (for example, UltraDockManager , UltraToolbarManager have a couple of SaveAsXml/LoadFromXml ).

0
source share

You can somehow save everything in a hidden textbox in a hidden form. When the user clicks the apply button, automatically open the text file and run the program in turn.

Example:

  • Line 1 may be the location of the image.
  • Line 2 may be a text field
  • Line 3 can be the word or number that the program uses to determine if the flag is true or false
-one
source share

All Articles