473,394 Members | 1,810 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,394 software developers and data experts.

passing values between two form by using property or delegate?

vanc
211 Expert 100+
I read many articles about this problem, but I found out that to pass values between one created form and one is not yet is created is fairly simple, it can be done by assign value to public variable or property in the form will be created.
eg:
form1 f1 = new form1;
f1.var1 = "abc";

form2 f2 = new form2;
f2.var2 = f1.var2;
f2.Show();

But then it's hard to pass a value from the second form back to the form that created it. I don't want to use static variable in the first form to be assigned by the second form. Since the property or delegate which is declared as Public in the first form will never be seen by the second form.

So how can we actually pass the values between two activated forms not by
using static variables?

Cheers.
Mar 27 '07 #1
5 3530
vanc
211 Expert 100+
I found a way to do this by using Delegate together with Event, it fairly simple and very impressive.
Say there are ReceiveForm and SendForm, ReceiveForm will instantiate SendForm and SendForm will talk with ReceiveForm by clicking event at button called btSend, whenever btSend in SendForm is clicked, it will update data in ReceiveForm.
----ReceiveForm----
SendForm f = new SendForm();
//add event to the local method to set data when SecondForm fires this event
f.DataUpdated += new SendForm.DataUpdateHandler(GetData);
f.Show();

//The CustomArgs class will be built to store data later
private void GetData(object sender, CustomArgs e)
{
ReceiveFormData = e.SendFormData;
}

-------SendForm-------
public delegate void DataUpdateHandler(object sender, CustomArgs e);
public event DataUpdataHandler DataUpdated;
//DataUpdateHandler is delegate's name, and DataUpdated is event's name

private void btSend_Click(object sender, SystemEventArgs e)
{
CustomArgs args = new CustomArgs(SendFormData); //store data here
//raise event
DataUpdated(this,args);
}

---------CustomArgs class here---------
public class CustomArgs
{
private string data;
public CustomArg(string input)
{
data = input;
}
//create property to send data back
public string SendFormData
{
get { return data; }
}
}

By this way 2 forms can talk together without creating any new form instances.
Mar 27 '07 #2
But, the first line of your code....

----ReceiveForm----
SendForm f = new SendForm();

Is this not creating an instance of SendForm in the ReceiveForm??
Mar 27 '07 #3
Hey hi friend,
I read ur problem. Do u wan to pass the values from one form to another. Is'nt it? Why don't u use Session variable for this purpose. B'coz I m also developing a website. In it I have done the same. Once the value is added in session variable it can be accessec on any page. U can pik it's syntax from help. If ur problem is something else , Plz convey.

Bye take care
Mar 27 '07 #4
vanc
211 Expert 100+
the first line of code is to create SecondForm, it has to be done somewhere right! From then on, these two forms can communicate for good.
The point is most of thing I read from forums is just create new form and set value thru property, or thru constructor, not about keeping many forms alive and talking together.

About session variable, I'm working in C# so I don't really know much about this.
Mar 27 '07 #5
If you wanted to pass a value from one form to another using a button:-
There are two forms...Form1 and Form2

Form1 {TextBox/Label & Button} Form2 {Label & TextBox}

In Form1 Button_Click eventhandler, create an instance of Form2
form2.visible = true;
Then fom2.TextBox1.Text = this.txtFrm1.Text;
This will do.. and if you wanna pass something back to form1 do the same in form1..

Regards,
TeenzoneZ
Mar 27 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

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) {...
2
by: Jeff Bush | last post by:
I am trying to create a generic Command object (following the Command design pattern) that allows me to specify a generic type, an object to operate on, and most importantly, a public Property on...
7
by: Richard Grant | last post by:
Hi. In c/C++ i can pass the address of a subroutine to another subroutine as an actual parameter How do I do that in VB .NET What should be the syntax for a parameter to receive the address of a...
6
by: Max | last post by:
Last time I tried to explain this on another forum it didn't go too well, so I'll try my best and if you know what I'm talking about then please tell me how to do this. I have a class, inside I...
0
by: Eric Sabine | last post by:
OK, I'm trying to further my understanding of threading. The code below I wrote as kind of a primer to myself and maybe a template that I could use in the future. What I tried to do was pass data...
3
by: Lonewolf | last post by:
Hi all, I'm having difficulties passing data back to managed class from my native class when the data is generated from within a native thread in the native class itself. I will give the following...
0
by: Haxan | last post by:
Hi, I have an unmanaged application that converts a function pointer to a delegate and then pass this as a parameter(delegate) to a managed function which then invokes it. Currently Im able to...
3
by: bsturg21 | last post by:
Hello, I have a windows form that has a series of linklabels on it, and I need to have each linklabel, when clicked, open a separate windows form that has a single paramter passed into it. The...
3
by: vidhyapriya | last post by:
Hi all I am developing windows application using vb.net.I want to pass values to open form.I am opening only one form when user click the buttons several times.Useing delegate i am passing...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...
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...

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.