Search code examples
c#winformsmodular

Fetch Data From Modular Form On Close


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?


Solution

  • 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