Add a static method to your form, for example:
public class MyDialog : Form
{
public static MyObject ShowAndReturnObject()
{
var dlg = new MyDialog();
if (new dlg.ShowDialog() == DialogResult.OK)
{
var obj =
return obj;
}
else
{
return null;
}
}
}
Now you can call this from your program:
var myObject = MyDialog.ShowAndReturnObject();
... and if they canceled the dialog, myObject will be null.
Now, having said all this, I believe that adding a property to your form class, which you then read after calling ShowDialog (), is the best approach.
source
share