Returning results from a Dialog.

  • Thread starter Thread starter Mark Broadbent
  • Start date Start date
M

Mark Broadbent

I know this has been discussed before, but rather than spend ages trawling
through old threads I thought I would start a new *quick* Q&A thread.

Q. What is the preferred method for returning data from a dialog to a parent
form.
Previously when I have been launching a dialog, I would pass a reference to
it's parent form in the ShowDialog method e.g. myDialog.ShowDialog(this)
from within the dialog I return the data by obtaining a reference from the
dialog's owner property and set the owner form value from within the
relevant dialog handler (e.g. the OK button).

However recently I have seen examples of getting the data from the dialog
from within the owner form code e.g.
FormDialog myForm = new FormDialog();
if (myForm.ShowDialog(this) == Forms.DialogResult.OK)
{
this.textBoxDialogResult.Text = myForm.textBoxReturnValue.Text;
}

Now the second method certainly seems slightly quicker (might be imagining
it), flexible, code efficient and transparent; but which is the recommended
method?

Q.2
Also another thing that needs to be done for this to work in either event is
for the member in the other instance that is being referred to from the
current one to be exposed as public (for instance as
"myForm.textBoxReturnValue"). Should this be accessed via public property
(as I expect) from a purest point of view or does it not really matter?
(Obviously using the second method, it would be the dialog which exposes the
public property -which I think is probably a more logical design)

Thanks in advance,

Br,

Mark.
 
Hi Mark,

I prefer the the second option. Create a dialog and, depending on the complexity, either pass the initial data in the constructor or set values using public properties in the dialog (like specifying InitialDirectory in the OpenFileDialog).
Similarly obtain the result from the same public properties in the dialog.

MyDialog d = new MyDialog();
d.Property = data;

if(d.ShowDialog == DialogResult.OK)
data = d.Property;

class MyDialog
{
public MyDialog(){}
}

I always use properties instead of directly accessing members of the dialog. The parent need it to perform some job, and gives it some data. How the internals of the dialog work is uninteresting, and should probably never be exposed to the outside.
Using properties it is easier to control the flow of data between the parent and the user using the dialog, as well as making the code easier to maintain.
 
That's what I thought. Definitely seems more logical and as you say easier
to maintain.

Thx m8.

Br,

Mark.

Morten Wennevik said:
Hi Mark,

I prefer the the second option. Create a dialog and, depending on the
complexity, either pass the initial data in the constructor or set values
using public properties in the dialog (like specifying InitialDirectory in
the OpenFileDialog).
Similarly obtain the result from the same public properties in the dialog.

MyDialog d = new MyDialog();
d.Property = data;

if(d.ShowDialog == DialogResult.OK)
data = d.Property;

class MyDialog
{
public MyDialog(){}
}

I always use properties instead of directly accessing members of the
dialog. The parent need it to perform some job, and gives it some data.
How the internals of the dialog work is uninteresting, and should probably
never be exposed to the outside.
Using properties it is easier to control the flow of data between the
parent and the user using the dialog, as well as making the code easier to
maintain.
 
Back
Top