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

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Passing parameters to and from a Form object

Hello,
I am a novice to C#.
Say I have two Windows Form: A and B (not a DialogBox).
Suppose I want from Form A object to create and display Form B object
(passing it parameters).
When closing the Form B object, I would like to get the return parameter/s.
Can anybody show me a way of doing it?

Thanks,
David
Nov 15 '05 #1
3 8755
David,

It's like any other method on any other object. Basically, in the
method of FormA, you would create an instance of FormB. Then, you can show
the form if you wish, calling the show method. However, you said you want
to get values returned. So, what you can do on FormB is have a method that
will show the form, as well as return any values that you need. Forms are
nothing more than objects, and you should treat them as such.

Also, if a form is closed, that doesn't mean that you lose the reference
that you have to it. It just means that it is not being shown (or it has
been destroyed, but that doesn't mean that the object wrapper is destroyed).

Hope this helps.

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

"David K." <ke*******@013.net.il> wrote in message
news:Ov**************@TK2MSFTNGP09.phx.gbl...
Hello,
I am a novice to C#.
Say I have two Windows Form: A and B (not a DialogBox).
Suppose I want from Form A object to create and display Form B object
(passing it parameters).
When closing the Form B object, I would like to get the return parameter/s. Can anybody show me a way of doing it?

Thanks,
David

Nov 15 '05 #2
I answered this in the forums over at devbuzz.
http://forums.devbuzz.com/tm.asp?m=2...mode=1&smode=1 and while this
is a compact framework site, the logic still holds. HTH,

Bill
"David K." <ke*******@013.net.il> wrote in message
news:Ov**************@TK2MSFTNGP09.phx.gbl...
Hello,
I am a novice to C#.
Say I have two Windows Form: A and B (not a DialogBox).
Suppose I want from Form A object to create and display Form B object
(passing it parameters).
When closing the Form B object, I would like to get the return parameter/s. Can anybody show me a way of doing it?

Thanks,
David

Nov 15 '05 #3
Jax
My preferred method is using delegates.
They're basically function/method pointers.
All you have to do is create a method in form1 that is expecting the data from form two (the args).
The delegate you use has to match the args and the return type of the method you want it to point to.
This example sends a reference of a button on form2 back to form1, obviously you can send any type you like.

namespace DelegateTest
{

public delegate void TheDelegate(object);
{
public class Form1 : System.Windows.Form
{
public Form1()
{
}
// this is the method we will point to
private void ReturnMethod(object o)
{
// do great things with object o
}
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private Form1_Load(object sender, System.EventArgs e)
{
Form2 f2 = new Form2(new TheDelegate(ReturnMethod));
f2.Show();
}
}
public class Form2 : System.Windows.Form
{
private TheDelegate f2Delegate;
public Form2(TheDelegate sender)
{
f2Delegate = sender;
}
private void SomeThingHappens(object sender, System.EventArgs e)
{
object o = sender;
f2Delegate(o); // This will call ReturnMethod in form1 and pass it object o.
}
}
}
}
Nov 15 '05 #4

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

Similar topics

2
by: zlatko | last post by:
There is a form in an Access Project (.adp, Access front end with SQL Server) for entering data into a table for temporary storing. Then, by clicking a botton, several action stored procedures...
3
by: domeceo | last post by:
can anyone tell me why I cannot pass values in a setTimeout function whenever I use this function it says "menu is undefined" after th alert. function imgOff(menu, num) { if (document.images) {...
12
by: Joel | last post by:
Hi all, Forgive me if I've expressed the subject line ill. What I'm trying to do is to call a c++ function given the following: a. A function name. This would be used to fetch a list of...
39
by: Mike MacSween | last post by:
Just spent a happy 10 mins trying to understand a function I wrote sometime ago. Then remembered that arguments are passed by reference, by default. Does the fact that this slowed me down...
11
by: Johnny | last post by:
I'm a rookie at C# and OO so please don't laugh! I have a form (fclsTaxCalculator) that contains a text box (tboxZipCode) containing a zip code. The user can enter a zip code in the text box and...
8
by: Johnny | last post by:
I'm a rookie at C# and OO so please don't laugh! I have a form (fclsTaxCalculator) that contains a text box (tboxZipCode) containing a zip code. The user can enter a zip code in the text box and...
0
by: Neelima Godugu | last post by:
Hi All, I have developed a windows forms user control, which I am going to host in Internet Explorer.. I am familiar with the security settings requirement inorder to do the above. I have...
13
by: Deano | last post by:
Apparently you can only do this with one value i.e Call MyAssetLocationZoom(Me!txtLocation, "Amend data") This runs; Public Sub MyAssetLocationZoom(ctl As Control, formName As String) On...
4
by: MicroMoth | last post by:
Hi, I'm trying to write a update method, in which when the user clicks the update button the update method is passed 10 form fields. Then a update SQL is run to update the database. My question...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.