473,657 Members | 2,513 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Return Values From Form?

Hello,

I have a windows forms application (VS2005) where I need to do the
following...

1. Startup with a main form.
2. Have the user to select a file and then bring up a second form
modally (Form1.ShowDial og()) so that the user can make custom
selections.
3. I can store the selections from the second form in many different
ways, but I have to get them back to the main form once the modal form
is closed. The selections are column header names, and right now I am
choosing to store them in a Generic List<string>.

I am doing fine so far, but now I need to figure out the best way to
get the selections back to the main form, once the user clicks
"Continue" and I call this.Close() on the second form.

I thought about creating the list and then passing a reference to the
modal form's constructor: "ref List<stringMyLi st". But this doesn't
work because the list is not scoped outside of the modal form's
constructor. And so I cannot interact with it when other events occur
on the form, like for example, a button click.

Another way would be create and modify the list within the second form
and then simply "return" it, but as far as I know one cannot do that
when using forms (would not be a problem with plain old functions).
There is not a return(MyList) that will work when closing a form,
right?

What would be a good way to tackle this?

Jul 10 '06 #1
11 3236
Joey,

Why not just expose a property on your form that will expose the
selections in a format that is easy for you to work with? The form
reference is still valid, and you can still access the properties exposed on
your form.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

<jo*********@to pscene.comwrote in message
news:11******** *************@7 5g2000cwc.googl egroups.com...
Hello,

I have a windows forms application (VS2005) where I need to do the
following...

1. Startup with a main form.
2. Have the user to select a file and then bring up a second form
modally (Form1.ShowDial og()) so that the user can make custom
selections.
3. I can store the selections from the second form in many different
ways, but I have to get them back to the main form once the modal form
is closed. The selections are column header names, and right now I am
choosing to store them in a Generic List<string>.

I am doing fine so far, but now I need to figure out the best way to
get the selections back to the main form, once the user clicks
"Continue" and I call this.Close() on the second form.

I thought about creating the list and then passing a reference to the
modal form's constructor: "ref List<stringMyLi st". But this doesn't
work because the list is not scoped outside of the modal form's
constructor. And so I cannot interact with it when other events occur
on the form, like for example, a button click.

Another way would be create and modify the list within the second form
and then simply "return" it, but as far as I know one cannot do that
when using forms (would not be a problem with plain old functions).
There is not a return(MyList) that will work when closing a form,
right?

What would be a good way to tackle this?

Jul 10 '06 #2
Could you explain a little more?

Nicholas Paldino [.NET/C# MVP] wrote:
Joey,

Why not just expose a property on your form that will expose the
selections in a format that is easy for you to work with? The form
reference is still valid, and you can still access the properties exposed on
your form.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

<jo*********@to pscene.comwrote in message
news:11******** *************@7 5g2000cwc.googl egroups.com...
Hello,

I have a windows forms application (VS2005) where I need to do the
following...

1. Startup with a main form.
2. Have the user to select a file and then bring up a second form
modally (Form1.ShowDial og()) so that the user can make custom
selections.
3. I can store the selections from the second form in many different
ways, but I have to get them back to the main form once the modal form
is closed. The selections are column header names, and right now I am
choosing to store them in a Generic List<string>.

I am doing fine so far, but now I need to figure out the best way to
get the selections back to the main form, once the user clicks
"Continue" and I call this.Close() on the second form.

I thought about creating the list and then passing a reference to the
modal form's constructor: "ref List<stringMyLi st". But this doesn't
work because the list is not scoped outside of the modal form's
constructor. And so I cannot interact with it when other events occur
on the form, like for example, a button click.

Another way would be create and modify the list within the second form
and then simply "return" it, but as far as I know one cannot do that
when using forms (would not be a problem with plain old functions).
There is not a return(MyList) that will work when closing a form,
right?

What would be a good way to tackle this?
Jul 10 '06 #3
Joey,

A form is an object, like anything else. Just define your type to
expose a property/method that you can call after ShowDialog returns which
will give you the values from the dialog.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

<jo*********@to pscene.comwrote in message
news:11******** **************@ 35g2000cwc.goog legroups.com...
Could you explain a little more?

Nicholas Paldino [.NET/C# MVP] wrote:
>Joey,

Why not just expose a property on your form that will expose the
selections in a format that is easy for you to work with? The form
reference is still valid, and you can still access the properties exposed
on
your form.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

<jo*********@t opscene.comwrot e in message
news:11******* **************@ 75g2000cwc.goog legroups.com...
Hello,

I have a windows forms application (VS2005) where I need to do the
following...

1. Startup with a main form.
2. Have the user to select a file and then bring up a second form
modally (Form1.ShowDial og()) so that the user can make custom
selections.
3. I can store the selections from the second form in many different
ways, but I have to get them back to the main form once the modal form
is closed. The selections are column header names, and right now I am
choosing to store them in a Generic List<string>.

I am doing fine so far, but now I need to figure out the best way to
get the selections back to the main form, once the user clicks
"Continue" and I call this.Close() on the second form.

I thought about creating the list and then passing a reference to the
modal form's constructor: "ref List<stringMyLi st". But this doesn't
work because the list is not scoped outside of the modal form's
constructor. And so I cannot interact with it when other events occur
on the form, like for example, a button click.

Another way would be create and modify the list within the second form
and then simply "return" it, but as far as I know one cannot do that
when using forms (would not be a problem with plain old functions).
There is not a return(MyList) that will work when closing a form,
right?

What would be a good way to tackle this?

Jul 10 '06 #4
public ModalForm : Form
{
...
public List<stringUser Choices
{
get
{
... build list of strings and return it ...
}
}
}

public Form : MainForm
{
...
private void Button_Click(ob ject sender, System.EventArg s e)
{
ModalForm modal = new ModalForm();
if (modal.ShowDial og() == DialogResult.OK )
{
List<stringuser Choices = modal.UserChoic es;
...
}
modal.Dispose() ;
}
}

jo*********@top scene.com wrote:
Could you explain a little more?

Nicholas Paldino [.NET/C# MVP] wrote:
Joey,

Why not just expose a property on your form that will expose the
selections in a format that is easy for you to work with? The form
reference is still valid, and you can still access the properties exposed on
your form.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m
Jul 10 '06 #5
Okay, I got it. I was thinking that once you called this.Close(), the
form went out of scope. Now I see. Thanks a lot.

Nicholas Paldino [.NET/C# MVP] wrote:
Joey,

A form is an object, like anything else. Just define your type to
expose a property/method that you can call after ShowDialog returns which
will give you the values from the dialog.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

<jo*********@to pscene.comwrote in message
news:11******** **************@ 35g2000cwc.goog legroups.com...
Could you explain a little more?

Nicholas Paldino [.NET/C# MVP] wrote:
Joey,

Why not just expose a property on your form that will expose the
selections in a format that is easy for you to work with? The form
reference is still valid, and you can still access the properties exposed
on
your form.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

<jo*********@to pscene.comwrote in message
news:11******** *************@7 5g2000cwc.googl egroups.com...
Hello,

I have a windows forms application (VS2005) where I need to do the
following...

1. Startup with a main form.
2. Have the user to select a file and then bring up a second form
modally (Form1.ShowDial og()) so that the user can make custom
selections.
3. I can store the selections from the second form in many different
ways, but I have to get them back to the main form once the modal form
is closed. The selections are column header names, and right now I am
choosing to store them in a Generic List<string>.

I am doing fine so far, but now I need to figure out the best way to
get the selections back to the main form, once the user clicks
"Continue" and I call this.Close() on the second form.

I thought about creating the list and then passing a reference to the
modal form's constructor: "ref List<stringMyLi st". But this doesn't
work because the list is not scoped outside of the modal form's
constructor. And so I cannot interact with it when other events occur
on the form, like for example, a button click.

Another way would be create and modify the list within the second form
and then simply "return" it, but as far as I know one cannot do that
when using forms (would not be a problem with plain old functions).
There is not a return(MyList) that will work when closing a form,
right?

What would be a good way to tackle this?
Jul 10 '06 #6
jo*********@top scene.com wrote:
Okay, I got it. I was thinking that once you called this.Close(), the
form went out of scope. Now I see. Thanks a lot.
Be careful though. If the form is opened using just .Show, then when
..Close is called, it is disposed automatically. When opened using
..ShowDialog, then you must dispose it yourself after it is closed. In
that case you can access its properties before you dispose it.

Jul 10 '06 #7
Good to know. So, I guess the GC will dispose of it when it goes out of
scope, so I don't have to call Dispose(), right?

Chris Dunaway wrote:
jo*********@top scene.com wrote:
Okay, I got it. I was thinking that once you called this.Close(), the
form went out of scope. Now I see. Thanks a lot.

Be careful though. If the form is opened using just .Show, then when
.Close is called, it is disposed automatically. When opened using
.ShowDialog, then you must dispose it yourself after it is closed. In
that case you can access its properties before you dispose it.
Jul 10 '06 #8
If you show it using .Show(), that is correct.

If you show it using .ShowDialog() then you should dispose of it
yourself.

jo*********@top scene.com wrote:
Good to know. So, I guess the GC will dispose of it when it goes out of
scope, so I don't have to call Dispose(), right?

Chris Dunaway wrote:
jo*********@top scene.com wrote:
Okay, I got it. I was thinking that once you called this.Close(), the
form went out of scope. Now I see. Thanks a lot.
>
Be careful though. If the form is opened using just .Show, then when
.Close is called, it is disposed automatically. When opened using
.ShowDialog, then you must dispose it yourself after it is closed. In
that case you can access its properties before you dispose it.
Jul 10 '06 #9

Joey,

I'd suggest a different route.

I'd have a backend object, used to hold items. And implement the Singleton
design pattern.

If you go to my blog:
http://sholliday.spaces.msn.com/
10/24/2005

I have a "Web Session Object Holder".

You can read about the concept there.
Here is a "winforms" version of that same code:

You can find the code to use it at the blog.

But basically, .. like in your Form2 ... cmdClose button, you can do
something like this:

MemoryDataStore mds = MemoryDataStore .GetInstance();
object testO = new object();
mds.Add ("mykeyname" , testO );
Ok.. then after you get back from the form2 you do this

MemoryDataStore mds = MemoryDataStore .GetInstance();
object oFromStore = mds.Remove("myk eyname") as object;
if(null!=oFromS tore)
{
//do something with o//
}

of course, change "object" to whatever object you're interested in.

Put your "mykeyname" in a constant somewhere..... to be consistent.

The object was written with 1.1 in mind. I haven't thought about rewriting
it with 2.0 Generics in mind.

public class MemoryDataStore //: IDataStore
{
private static MemoryDataStore singletonInstan ce = null;
private HybridDictionar y m_memoryStore = null;
//private Hashtable m_memoryStore = null;

private MemoryDataStore ( )
{
m_memoryStore = new HybridDictionar y(); // Implements IDictionary by
using a ListDictionary while the collection is small, and then switching to
a Hashtable when the collection gets large.
//m_memoryStore = new Hashtable(); // Implements IDictionary by using a
ListDictionary while the collection is small, and then switching to a
Hashtable when the collection gets large.
}
public static MemoryDataStore GetInstance( )
{
if( null == singletonInstan ce )
{
singletonInstan ce = new MemoryDataStore ( );
}
return singletonInstan ce;
}
public void Clear( )
{
m_memoryStore.C lear( );
}

public void Add( string key, object value )
{

if( m_memoryStore.C ontains( key ) )
{
m_memoryStore.R emove( key );
}

m_memoryStore.A dd( key, value );
}

public object Remove( string key )
{

// see
http://java.sun.com/j2se/1.4.2/docs/...va.lang.Object)
// for java method, which returns the object you removed, in case
// you want to do something with it

object returnObject = null;
if (null != this.m_memorySt ore)
{
if( m_memoryStore.C ontains( key ) )
{
returnObject = this.m_memorySt ore[key];
m_memoryStore.R emove( key );
}

}
return returnObject;

}

public object this[ string key ]
{
get
{
if( null != m_memoryStore[ key ] )
{
return m_memoryStore[ key ];
}
return null;
}
}

public int Size
{
get
{
if( null != m_memoryStore )
{
return m_memoryStore.C ount ;
}
return 0;
}
}

}



<jo*********@to pscene.comwrote in message
news:11******** *************@7 5g2000cwc.googl egroups.com...
Hello,

I have a windows forms application (VS2005) where I need to do the
following...

1. Startup with a main form.
2. Have the user to select a file and then bring up a second form
modally (Form1.ShowDial og()) so that the user can make custom
selections.
3. I can store the selections from the second form in many different
ways, but I have to get them back to the main form once the modal form
is closed. The selections are column header names, and right now I am
choosing to store them in a Generic List<string>.

I am doing fine so far, but now I need to figure out the best way to
get the selections back to the main form, once the user clicks
"Continue" and I call this.Close() on the second form.

I thought about creating the list and then passing a reference to the
modal form's constructor: "ref List<stringMyLi st". But this doesn't
work because the list is not scoped outside of the modal form's
constructor. And so I cannot interact with it when other events occur
on the form, like for example, a button click.

Another way would be create and modify the list within the second form
and then simply "return" it, but as far as I know one cannot do that
when using forms (would not be a problem with plain old functions).
There is not a return(MyList) that will work when closing a form,
right?

What would be a good way to tackle this?

Jul 12 '06 #10

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

Similar topics

3
4532
by: Varun | last post by:
Hi There, I have a form("myRequest.asp") and the values from it are retrieved into the page ("output_Print.asp") on which I have two buttons('Save As Complete' and 'Save As Incomplete'). When the 'Save as Incomplete' button is Clicked the form will be going to the "SaveAsincomplete.asp" without validation of the fields. And when the 'save as complete' is clicked certain fileds are to be validated and by the function return value, if false...
0
3439
by: Mike Copeland | last post by:
In the following code I am calculating 2 values when the "Compute Distance" button is pressed. I would like to find a way to "return" both value to the form so I can show both when the calculation is made. (one is merely the doubling of the computed result.) I have cobbled this form from several places, and I don't remember enough of HTML or JavaScript to know where to seek additional information. The code below does the calculation,...
15
2796
by: Nerox | last post by:
Hi, If i write: #include <stdio.h> int foo(int); int main(void){ int a = 3; foo(a); }
1
4669
by: Jack Addington | last post by:
I have a 3rd party object that fires an itemchanged event when someone edits some data on a form. This event has a custom eventArgs that has a field called ActionCode. In the code of the event, if you set this ActionCode to various values the control will respond in various ways (reject, reject change field, accept, etc). I am trying to understand exactly how that works as I am trying to extend the control and add more processing s...
5
2064
by: Edward Diener | last post by:
I am gathering from the documentation that return values from __events are not illegal but are frowned upon in .NET. If this is the case, does one pass back values from an event handler via "in/out" or "out" parameters ? Or is it simply that events are just notifications and are not interested in any values which event handlers might be able to return ? If the latter is the case, the event model in .NET appears to have a great...
1
2121
by: John Dann | last post by:
VB.Net 2005: I have a class that performs some functions and then needs to update some labels on the main form (from which it is called) with several pieces of information, some text, some datetime etc, according to the results of the calculations. I can see at least a couple of ways of doing this: 1. Define a structure that contains all the items of information that
4
4969
by: d3vkit | last post by:
I have a form on my page, and some javascript which uses ajax to submit the form, and then opens the new page in a div using ajax so there is no refresh. This works fine. But the problem is this: all values seem to pass except what the submit buttons say. I have a preview button and a finish button, and need to check which was pressed. I can add a checkbox for submit, but that's annoying. I'm using mootools for the ajax call, so it might not...
2
2907
by: Blackmore | last post by:
I am trying to use javascript to calculate the difference between two form inputted dates and return the result to another form object. When I load up the page with the function on my web browser the form does not load and I get a message to say that the page contains errors, presumably as the function is not initialising or being referenced properly. Can anyone help, is the function scripted correctly and are the variables I am attempting to...
1
3027
by: brendwal | last post by:
Hi, I am stumped at how to do this: I have a simple webform that I want the user to enter one value (an address). I then want the form to send those values to a PHP script that processes the address and returns 2 numeric values back to the original webform populating two textboxes in a second form on the orginal page. I have the PHP script working to calculate the values, but I don't know how to send them properly and return the new...
0
8324
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8842
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8513
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8617
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7352
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6176
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4173
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
1970
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1733
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.