473,466 Members | 1,661 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Returning results from a Dialog.

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.

Nov 16 '05 #1
2 11648
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.

--
Happy coding!
Morten Wennevik [C# MVP]
Nov 16 '05 #2
That's what I thought. Definitely seems more logical and as you say easier
to maintain.

Thx m8.

Br,

Mark.

"Morten Wennevik" <Mo************@hotmail.com> wrote in message
news:opscgnacbkklbvpo@morten_x.edunord...
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.
--
Happy coding!
Morten Wennevik [C# MVP]

Nov 16 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Robin Hammond | last post by:
Can anybody tell me why a) when running a stored proc from an asp page to return a recordset the command succeeds if the sp queries an existing table directly, but b) if the stored proc populates...
2
by: Gordon Airporte | last post by:
The dialogs in tkColorChooser, tkFileDialog, etc. return useful values from their creation somehow, so I can do stuff like this: filename = tkFileDialog.askopenfilename( master=self ) I would...
2
by: Brian Henry | last post by:
If i have a window showing like this <script>window.showModalDialog('../weblinks/default.aspx','','unadorned:yes' );</script> how can i get it to return a value to its calling page? I want to...
10
by: KeithRaginF | last post by:
Hello I am trying to return the contents of an Excel file. However, I don't want to just redirect the request to the Excel file since it does not reside in a location where that is appropriate. ...
0
by: colinhumber | last post by:
I'm having some issues with my full text indexes in MySQL 3.23.58. I'm using I have a table with a full text index on the 'content' column. There are some words that are returning results just fine...
5
by: jodyblau | last post by:
I'm new at this stuff, so let me know if I am approaching this incorrectly. I have a form that allows the user to select a document that is in one folder, and copy it into another folder. I...
1
by: Curious Trigger | last post by:
Hi there, programming with Visual Studio 2005 and ASP.NET 2.0 I want to open a modal dialog from Default.aspx. I searched the net and many newsgroups but I couldn't find any solution. First I...
7
by: Thomas Lenz | last post by:
Please consider the following code snippet: string myfunction() { ostringstream oss; oss << "junk"; // do something more with oss; I can't make it const... return oss.str(); } Is the...
1
Eclipse
by: Eclipse | last post by:
G'day all Can anyone explain the difference in the results to me as I don't understand why specifying the directory name in two different ways could give a different answer. In CODE 1 below i...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.