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
.