C# - Memorizing the forms controls config.

EFileTahi-A

Contributor
Joined
Aug 8, 2004
Messages
623
Location
Portugal / Barreiro
Imagine that Form1 opens Form2 using ".ShowDialog()". Now, Form2 only haves CheckBoxes. What I need is to preserve the "checks" configuration after closing and re-opening Form2.

What do you think is best?:

1 - Memorizing the "CheckBoxes" config. in a stringVar or something...
2 - Not closing the form2 when wanted, but, making it invisible...

If theres another simple way apart from these two, please let me know about it!
 
What I always do with such a 'setting' dialog (dialog that is shown modal to enter some values and then closed again, where the calling function needs to read out the values set) is a 3rd option:

Add public properties to the form that is shown with both get and set.
I set the values before showing (to display the current settings of the item), and read them again after the showdialog returns. In the form itself, during the load, the values of the properties are read and UI updated, during the Closing event, the checkboxes are analysed and the members updated.
To put this in code it would be something like this:

The Caller
Code:
private void Settings_Click(<regular button click event arguments that i dont remember> )
{
 //create the dialog
 SettingsDialog newDialog = new SettingsDialog();
 //Update the dialog with the current settings we have
 newDialog.MySetting1 = mMySetting1;
 newDialog.MySetting2 = mMySetting2;
 //display the dialog
 newDialog.ShowDialog();
 //copy the new settings from the dialog to the member variables of this  caller
 mMySetting1 = newDialog.MySetting1;
 mMySetting2 = newDialog.MySetting2;
}

The SettingsDialog, only the interesting bits ;)
Code:
//some member variables to store the value of the settings
private int mMySetting1;
private int mMySetting2;

//get/set property methods to access the settings
public int MySetting1
{
 get
 {
  return mMySetting1
 }
 set
 {
  mMySetting1 = value;
 }
}
public int MySetting2
{
 get
 {
  return mMySetting2
 }
 set
 {
  mMySetting2 = value;
 }
}

//now during the load, this is called automatically when the dialog is shown,
//update the userinterface to show the settings that are active at this //moment
//dont do this in the constructor of the form, as the settings are not yet set 
// at that moment
private void SettingsDialog_Load( <form load event arguments>)
{
 txtSetting1.Text = mMySetting1.ToString();
 txtSetting2.Text = mMySetting2.ToString();
}

//When closing the window, store the settings that are made
private void SettingsDialog_Closing(<form closing event arguments> )
{
 mMySetting1 = int.Parse(txtSetting1.Text);
 mMySetting2 = int.Parse(txtSetting2.Text);
}
this last code window is a bit big, make sure to scroll down in it to the Load and Closing event handlers

No tricks with hiding forms, no string parsing either. Just readable properties of the SettingsDialog object. If you ever need an extra property in a later state of the project, it will be quite easy to add as well.
What you use in this way, is the fact that although the window itself might not be visible, the object newDialog is still valid after it is closed (or before it is shown).

Just make sure not to copy the exact code, I didnt remember the exact parameters in the events while I typed this, and was to lazy to code it in the visual studio ;).
 
Thanks Wile, extremely usefull stuff!!!

Yet, I never used DialogSettings object before, so, where do I place the first and second piece of code?

I mean the first piece of code should be the button on form1 to open form2?
And where should I place the second piece of code?

Sry, kinda dazzy here, and am not an C# expert ;)
 
Last edited:
If it is config data woudl you not be safer saving it off to a config file/database so that it will be preserved and can be used the next time the app starts etc..
 
donnacha said:
If it is config data woudl you not be safer saving it off to a config file/database so that it will be preserved and can be used the next time the app starts etc..

Not talking about the way to store the windows config, but the way memorizing controls in memory works by opening and closing a for by preserving its last stats...
 
The DialogSettings object is not a .NET object, it is a new window that you can create yourself. I'll attach some real code to show this.

In the plugin.cs, look for the ShowAdvancedOptions() method. It works with a popup window (which is the other file in the zip) like I described above.
 

Attachments

Back
Top