0

I am creating a modular form in C#, and would like to know how to retrieve the data from the modular form upon closing for usage on the main form?

Alan W
  • 291
  • 1
  • 5
  • 18

1 Answers1

1

since you are using ShowDialog you can use a using block. If you set the DialogResult on close as well, you can make sure you only use these details if closed correctly

//OnClosing...
DialogResult = DialogResult.OK;

using(var myFormInstance = new myForm())
{

    myFormInstance.ShowDialog()  //<-- Only if you dont need to check dlg result
    //whilst in here myFormInstance will give me access to the variables
    if(myFormInstance.ShowDialog() == DialogResult.OK) //<-If you do check result
    {
       //success
    }
}

Just provide public methods or properties to values that you wish to retrieve and then call them and use them accordingly

Sayse
  • 42,633
  • 14
  • 77
  • 146
  • No, that should be inside your "modular form" it has an OnClosing event ([like this](http://msdn.microsoft.com/en-us/library/system.windows.forms.form.onclosing(v=vs.110).aspx)), but then you may have a button that closes the form, wherever you close the form, add the `DialogResult` line above it. Note that you do not have to use this, its just a suggestion to make sure you don't use values when your user clicked a cancel button for example. – Sayse May 29 '14 at 07:13
  • Gotcha. Another question, whenever I try to use the using block, the compiler complains that the type must be implicitly convertible to an IDisposable object. Do you know why that might be? – Alan W May 29 '14 at 07:17
  • @AlanW - Appologies I may have my example slightly skewed.. I'll edit – Sayse May 29 '14 at 07:19