473,659 Members | 3,420 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

passing values between two form by using property or delegate?

vanc
211 Recognized Expert New Member
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 3539
vanc
211 Recognized Expert New Member
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.DataUp dateHandler(Get Data);
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 DataUpdateHandl er(object sender, CustomArgs e);
public event DataUpdataHandl er DataUpdated;
//DataUpdateHandl er is delegate's name, and DataUpdated is event's name

private void btSend_Click(ob ject sender, SystemEventArgs e)
{
CustomArgs args = new CustomArgs(Send FormData); //store data here
//raise event
DataUpdated(thi s,args);
}

---------CustomArgs class here---------
public class CustomArgs
{
private string data;
public CustomArg(strin g 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
avkdsiva
23 New Member
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
Snehal Rathod
6 New Member
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 Recognized Expert New Member
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
Teenzonez
36 New Member
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.T ext = this.txtFrm1.Te xt;
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
14929
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) { document.images.src = eval("mt" +menu+ ".src") } alert("imgOff_hidemenu"); hideMenu=setTimeout('Hide(menu,num)',500);
2
3554
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 that object for getting and setting the value in question. Currently the only approach I've found to work is to use delegates along with Get and Set functions (instead of using a Property). See code below. Anyone have a better approach than...
7
9558
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 subroutine Let's say theres a sub that creates buttons and I want it to receive as a parameter the address of the sub that handles the OnClick event for the button being created How do I pass such a parameter Thanks in advance Richar
6
3942
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 have some public functions and private variables. Inside the class I also have a declaration of a new form object. One of the functions of the class takes that form object, shows it with showdialog and the basically passes the control to the form...
0
1478
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 into a background thread and get other data out and also update the main thread on which the main form was created. It seems to work fine. The basic function of the app is cheesy, I didn't spend any time on exception handling. northwind.mdb...
3
3665
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 runtime error, " Attempting to call into managed code without transitioning out first. Do not attempt to run managed code inside low-level native extensibility points, such as the vectored exception handler, since doing so can cause corruption...
0
2010
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 jump to this unmanaged function, but the values of the parameters inside this function Im seeing are not correct(they have some garbage values). //unmanaged class (C++ application)
3
4680
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 form that has the System.Windows.Forms.LinkLabel controls on it is in a different project and under a different namespace from the file where the LinkLabel_LinkClicked events are, so I can't just do frm.ShowDialog under the LinkClicked method. ...
3
6444
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 values for first time.Next time when user click the button i want to pass value to open form.How to pass value to open form.Can anyone help me plz.Thanx in advance Code I used, Form1 Code: Public Delegate Sub GreetingDelegate(ByVal MsgString As...
0
8428
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8335
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
8747
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8627
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
5649
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4335
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2752
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 we have to send another system
2
1976
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1737
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.