473,473 Members | 2,320 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Returning values from a form

I have an application I'm working on where one form will open another.
This newly opened form needs to return a value based on the users actions.
My forms are not modal. I have seen discussions about overridding the
DialogResult, however I believe this will only work in a modal form
situation.

I have code where I've added a custom event to the child form called
ReturnValueSet. The form that opens this form can then subscribe to this
event and trap the value that was set if it wishes. The child form then
must be coded to raise this event when it is closing. This method works,
but I would prefer to use a more standard approach is there is one.

Does anyone have suggestions?

Thanks
Dan
Sep 1 '08 #1
7 2188
On Mon, 01 Sep 2008 11:50:46 -0700, Dan Tallent <sp**@microsoft.comwrote:
[...]
I have code where I've added a custom event to the child form called
ReturnValueSet. The form that opens this form can then subscribe to
this
event and trap the value that was set if it wishes. The child form then
must be coded to raise this event when it is closing. This method
works,
but I would prefer to use a more standard approach is there is one.
If you just want the client form to retrieve the value when the child form
is closed, then I'd say the client form should just subscribe to the
FormClosed event. Then the child form should set some property that the
client form can retrieve when the FormClosed event is raised (the child
form would of course make sure this property is suitably set when it's
closed, before the FormClosed event is raised).

That said, other than the name of the event, what you've done is actually
a reasonably standard approach, depending on the semantics you want. The
..NET convention would be to have a property, and then an event with the
same name as the property, plus "Changed".

You could either only raise the "Changed" event when the form closes, or
you could allow for the client form to monitor changes as the user
provides input. Either way, that would be reasonable too.

Pete
Sep 1 '08 #2
Thanks for the quick response.

If I use the FormClosed event does this cause a potential issue that the
form may of already been released and therefore the property being
unavailable?

If using the FormClosed event is safe, do you know if the code within the
child forms FormClosed event fires before it raises the event to the parent?

Thanks
Dan


"Peter Duniho" <Np*********@nnowslpianmk.comwrote in message
news:op***************@petes-computer.local...
On Mon, 01 Sep 2008 11:50:46 -0700, Dan Tallent <sp**@microsoft.com>
wrote:
>[...]
I have code where I've added a custom event to the child form called
ReturnValueSet. The form that opens this form can then subscribe to
this
event and trap the value that was set if it wishes. The child form then
must be coded to raise this event when it is closing. This method
works,
but I would prefer to use a more standard approach is there is one.

If you just want the client form to retrieve the value when the child form
is closed, then I'd say the client form should just subscribe to the
FormClosed event. Then the child form should set some property that the
client form can retrieve when the FormClosed event is raised (the child
form would of course make sure this property is suitably set when it's
closed, before the FormClosed event is raised).

That said, other than the name of the event, what you've done is actually
a reasonably standard approach, depending on the semantics you want. The
.NET convention would be to have a property, and then an event with the
same name as the property, plus "Changed".

You could either only raise the "Changed" event when the form closes, or
you could allow for the client form to monitor changes as the user
provides input. Either way, that would be reasonable too.

Pete

Sep 1 '08 #3
Dan Tallent wrote:
Thanks for the quick response.

If I use the FormClosed event does this cause a potential issue that the
form may of already been released and therefore the property being
unavailable?

If using the FormClosed event is safe, do you know if the code within the
child forms FormClosed event fires before it raises the event to the parent?

Thanks
Dan


"Peter Duniho" <Np*********@nnowslpianmk.comwrote in message
news:op***************@petes-computer.local...
>On Mon, 01 Sep 2008 11:50:46 -0700, Dan Tallent <sp**@microsoft.com>
wrote:
>>[...]
I have code where I've added a custom event to the child form called
ReturnValueSet. The form that opens this form can then subscribe to
this
event and trap the value that was set if it wishes. The child form then
must be coded to raise this event when it is closing. This method
works,
but I would prefer to use a more standard approach is there is one.
If you just want the client form to retrieve the value when the child form
is closed, then I'd say the client form should just subscribe to the
FormClosed event. Then the child form should set some property that the
client form can retrieve when the FormClosed event is raised (the child
form would of course make sure this property is suitably set when it's
closed, before the FormClosed event is raised).

That said, other than the name of the event, what you've done is actually
a reasonably standard approach, depending on the semantics you want. The
.NET convention would be to have a property, and then an event with the
same name as the property, plus "Changed".

You could either only raise the "Changed" event when the form closes, or
you could allow for the client form to monitor changes as the user
provides input. Either way, that would be reasonable too.

Pete

You could use the FormClosing event.

Sep 1 '08 #4
Your client (or parent) form won't have released the instance, so the
instance count is not zero, and therefore it is not disposed. You won't
have an issue getting a property on the object from your instance.

"Dan Tallent" <sp**@microsoft.comwrote in message
news:e$**************@TK2MSFTNGP04.phx.gbl...
Thanks for the quick response.

If I use the FormClosed event does this cause a potential issue that the
form may of already been released and therefore the property being
unavailable?

If using the FormClosed event is safe, do you know if the code within the
child forms FormClosed event fires before it raises the event to the
parent?

Thanks
Dan


"Peter Duniho" <Np*********@nnowslpianmk.comwrote in message
news:op***************@petes-computer.local...
>On Mon, 01 Sep 2008 11:50:46 -0700, Dan Tallent <sp**@microsoft.com>
wrote:
>>[...]
I have code where I've added a custom event to the child form called
ReturnValueSet. The form that opens this form can then subscribe to
this
event and trap the value that was set if it wishes. The child form
then
must be coded to raise this event when it is closing. This method
works,
but I would prefer to use a more standard approach is there is one.

If you just want the client form to retrieve the value when the child
form is closed, then I'd say the client form should just subscribe to the
FormClosed event. Then the child form should set some property that the
client form can retrieve when the FormClosed event is raised (the child
form would of course make sure this property is suitably set when it's
closed, before the FormClosed event is raised).

That said, other than the name of the event, what you've done is actually
a reasonably standard approach, depending on the semantics you want. The
.NET convention would be to have a property, and then an event with the
same name as the property, plus "Changed".

You could either only raise the "Changed" event when the form closes, or
you could allow for the client form to monitor changes as the user
provides input. Either way, that would be reasonable too.

Pete

Sep 1 '08 #5
Sounds good.. now I have a plan.

Thanks
Dan

"Family Tree Mike" <Fa************@ThisOldHouse.comwrote in message
news:uw**************@TK2MSFTNGP03.phx.gbl...
Your client (or parent) form won't have released the instance, so the
instance count is not zero, and therefore it is not disposed. You won't
have an issue getting a property on the object from your instance.

"Dan Tallent" <sp**@microsoft.comwrote in message
news:e$**************@TK2MSFTNGP04.phx.gbl...
>Thanks for the quick response.

If I use the FormClosed event does this cause a potential issue that the
form may of already been released and therefore the property being
unavailable?

If using the FormClosed event is safe, do you know if the code within the
child forms FormClosed event fires before it raises the event to the
parent?

Thanks
Dan


"Peter Duniho" <Np*********@nnowslpianmk.comwrote in message
news:op***************@petes-computer.local...
>>On Mon, 01 Sep 2008 11:50:46 -0700, Dan Tallent <sp**@microsoft.com>
wrote:

[...]
I have code where I've added a custom event to the child form called
ReturnValueSet. The form that opens this form can then subscribe to
this
event and trap the value that was set if it wishes. The child form
then
must be coded to raise this event when it is closing. This method
works,
but I would prefer to use a more standard approach is there is one.

If you just want the client form to retrieve the value when the child
form is closed, then I'd say the client form should just subscribe to
the FormClosed event. Then the child form should set some property that
the client form can retrieve when the FormClosed event is raised (the
child form would of course make sure this property is suitably set when
it's closed, before the FormClosed event is raised).

That said, other than the name of the event, what you've done is
actually a reasonably standard approach, depending on the semantics you
want. The .NET convention would be to have a property, and then an
event with the same name as the property, plus "Changed".

You could either only raise the "Changed" event when the form closes, or
you could allow for the client form to monitor changes as the user
provides input. Either way, that would be reasonable too.

Pete


Sep 1 '08 #6
On Mon, 01 Sep 2008 12:38:19 -0700, Dan Tallent <sp**@microsoft.comwrote:
Thanks for the quick response.

If I use the FormClosed event does this cause a potential issue that the
form may of already been released and therefore the property being
unavailable?
As Mike says, the object itself will still be around (though not for the
reason he states). However, it is true that the form will have been
disposed by that time, so your property should not access the things
within the form that themselves are disposed when the form is disposed (in
particular, the various controls).

Typically, you'd address this by keeping the data of interest in a member
field that is updated either as the user provides input to the form, or by
simply initializing it in the OnFormClosing() override in the child form
(depending on when you want the property to be valid).
If using the FormClosed event is safe, do you know if the code within the
child forms FormClosed event fires before it raises the event to the
parent?
The FormClosed event is raised by the base OnFormClosed() method. The
OnFormClosing() method is called before the OnFormClosed(), so assuming
that's where you initialize the property, it will be valid when an event
raised by OnFormClosed() is raised, regardless of where in OnFormClosed()
you raise the event. If you somehow initialize the property in
OnFormClosed(), you'll want to do so before calling the base
OnFormClosed().

Pete
Sep 1 '08 #7
Peter, I think I follow what you are saying, but didn't realize that was
happening behind the scenes. Thanks for educating me today!

Mike

"Peter Duniho" <Np*********@nnowslpianmk.comwrote in message
news:op***************@petes-computer.local...
On Mon, 01 Sep 2008 12:38:19 -0700, Dan Tallent <sp**@microsoft.com>
wrote:
>Thanks for the quick response.

If I use the FormClosed event does this cause a potential issue that the
form may of already been released and therefore the property being
unavailable?

As Mike says, the object itself will still be around (though not for the
reason he states). However, it is true that the form will have been
disposed by that time, so your property should not access the things
within the form that themselves are disposed when the form is disposed (in
particular, the various controls).

Typically, you'd address this by keeping the data of interest in a member
field that is updated either as the user provides input to the form, or by
simply initializing it in the OnFormClosing() override in the child form
(depending on when you want the property to be valid).
>If using the FormClosed event is safe, do you know if the code within the
child forms FormClosed event fires before it raises the event to the
parent?

The FormClosed event is raised by the base OnFormClosed() method. The
OnFormClosing() method is called before the OnFormClosed(), so assuming
that's where you initialize the property, it will be valid when an event
raised by OnFormClosed() is raised, regardless of where in OnFormClosed()
you raise the event. If you somehow initialize the property in
OnFormClosed(), you'll want to do so before calling the base
OnFormClosed().

Pete
Sep 1 '08 #8

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

Similar topics

6
by: Krackers | last post by:
How do you write a function which returns a reference to an array. I can only get a function to return a copy of the array itself. I've had a look at some other threads in this group an the return...
2
by: Graham Blandford | last post by:
Hi all, Quickie - I hope. I already know how to use a forms New() Sub to receive parameters from a calling class - but I don;t know how to return values... Anyone know the recommended method...
18
by: cppaddict | last post by:
Hi, Is it considered bad form to have the subscript operator return a const reference variable? If not, what is the proper way to do it? My question was prompted by the code below, my...
1
by: Ben | last post by:
I have a Tabular form bound to a table. The purpose of this form is to get times from a timer and record them in a field. The timer dumps the time automatically through a serial port. When the time...
2
by: MLH | last post by:
I have the following tasks to perform on an unbound form. I want them to run every time I return to the form. For reasons not at issue here, I do not close the form with a docmd.close command. I...
1
by: John Chorlton | last post by:
I've been attempting to pass a chunk of data back from a child Windows form using public properties on the form and have been getting some odd errors. I wanted to return a row of data to avoid...
1
by: vinodkus | last post by:
I M BEGINNER IN ASP I WANT TO RETURN TOTAL RECORDS FROM A TABLE. THERE ARE TWO FORMS CLASS1.ASP AND CLASS2.ASP THROUGH FIRST FORM I JUST POST THE NAME OF TABLE SO I M WRITING THE CODE OF...
0
by: cyberdawg999 | last post by:
Greetings all in ASP land I have overcome one obstacle that took me 2 weeks to overcome and I did it!!!!! I am so elated!! thank you to all who invested their time and energy towards helping me...
1
by: Max58kl | last post by:
Hi I am trying to setup a form that automatically sends the form values via email to a specific address. I have uploaded the script, which when I run loads a page with the following error...
7
Haitashi
by: Haitashi | last post by:
I have a form that calls a function using the onClick event in the submit button. I would like this function to return false if the username is taken. The the original function, which I've tested,...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
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: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
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 ...
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.