How to transfer data between two form
-
How to transfer data between two form ? I found few articles but it is only transmitted in one direction instructions
-
How to transfer data between two form ? I found few articles but it is only transmitted in one direction instructions
An easy is with custom events. In a simple implementation, you add an event to each form (e.g., "FormDataChanged") that gets triggered when the data changes that you want to transfer. You can store that data in a static variable in program. Then pass that data to the other form whenever you like.
public static Program
{private static object _myTransferData = new object();
//...
static int Main(arg, args)
{
MyForm1.DataChanged += new FormDataChanged;
}private static void FormDataChanged(object sender, EventArgs e)
{
// when you instantiate form2 later on, you can use
//_myTransferData to pass your values
_myTransferData = MyForm1.DataStorage;
}}
-
An easy is with custom events. In a simple implementation, you add an event to each form (e.g., "FormDataChanged") that gets triggered when the data changes that you want to transfer. You can store that data in a static variable in program. Then pass that data to the other form whenever you like.
public static Program
{private static object _myTransferData = new object();
//...
static int Main(arg, args)
{
MyForm1.DataChanged += new FormDataChanged;
}private static void FormDataChanged(object sender, EventArgs e)
{
// when you instantiate form2 later on, you can use
//_myTransferData to pass your values
_myTransferData = MyForm1.DataStorage;
}}
Oh, dear, oh dear. You were doing so well, right up to the "You can store that data in a static variable in program". What happens if there are two instances of the form, with data changing? What happens if the data changes again? Static is generally a bad idea, particularly when you are passing the information to another class. Either keep it local to your class instance, and let the handler pick it up from the
sender
parameter if it is just the latest info they need, or also create a custom EventArgs based class that you hand the changed data though with when you signal the event.Ideological Purity is no substitute for being able to stick your thumb down a pipe to stop the water
-
Oh, dear, oh dear. You were doing so well, right up to the "You can store that data in a static variable in program". What happens if there are two instances of the form, with data changing? What happens if the data changes again? Static is generally a bad idea, particularly when you are passing the information to another class. Either keep it local to your class instance, and let the handler pick it up from the
sender
parameter if it is just the latest info they need, or also create a custom EventArgs based class that you hand the changed data though with when you signal the event.Ideological Purity is no substitute for being able to stick your thumb down a pipe to stop the water
I agree and my original example was setting form1.foo = form2.foo. However, I decided on a field to show how you can store data between instantiations if you want. In this case, any member of Program has to be static since it's a static class. Regardless, your point is generally a good one.
-
How to transfer data between two form ? I found few articles but it is only transmitted in one direction instructions
imho a question like this would be better posted in the Q/A secion, with taqs included: is this Windows Forms C# ? Second, by "transfer data" you could mean several things: automatic update (binding) one-way, or two-way, or: in response to a user action on one or both forms (like the user clicks a button). You need to specify if the data being passed from Form1 to Form2 is a "horse of different color" than the data being passed from Form2 and Form1: or, if they are the same exact structure, then you need to tell us you are synchronizing data between two forms. Another plausible scenario is that both Forms are being streamed data from some real-time source, or that a control on one Form is bound to a data-provider of some type, and you need to take action everytime that Form is updated on the other Form. The more specific the questions, the more detailed the scenario you take the time to describe: behold: the more focused answers you will get :) best, Bill
"Humans are amphibians ... half spirit and half animal ... as spirits they belong to the eternal world, but as animals they inhabit time. This means that while their spirit can be directed to an eternal object, their bodies, passions, and imaginations are in continual change, for to be in time, means to change. Their nearest approach to constancy, therefore, is undulation: the repeated return to a level from which they repeatedly fall back, a series of troughs and peaks.” C.S. Lewis
-
imho a question like this would be better posted in the Q/A secion, with taqs included: is this Windows Forms C# ? Second, by "transfer data" you could mean several things: automatic update (binding) one-way, or two-way, or: in response to a user action on one or both forms (like the user clicks a button). You need to specify if the data being passed from Form1 to Form2 is a "horse of different color" than the data being passed from Form2 and Form1: or, if they are the same exact structure, then you need to tell us you are synchronizing data between two forms. Another plausible scenario is that both Forms are being streamed data from some real-time source, or that a control on one Form is bound to a data-provider of some type, and you need to take action everytime that Form is updated on the other Form. The more specific the questions, the more detailed the scenario you take the time to describe: behold: the more focused answers you will get :) best, Bill
"Humans are amphibians ... half spirit and half animal ... as spirits they belong to the eternal world, but as animals they inhabit time. This means that while their spirit can be directed to an eternal object, their bodies, passions, and imaginations are in continual change, for to be in time, means to change. Their nearest approach to constancy, therefore, is undulation: the repeated return to a level from which they repeatedly fall back, a series of troughs and peaks.” C.S. Lewis
-
-
:doh: Did you try?
//Form1:
private string valueFromSecondForm;public string ValueFromSecondForm
get { return value; }
set { valueFromSecondForm = value; }private void button1_Click(object sender, EventArgs e)
{
Form2 frm = new Form2(this);
frm.Show();
}//Form2:
public partial class Form2 : Form
{
Form opener;public Form2(Form parentForm) { InitializeComponent(); opener = parentForm; } private void button1\_Click(object sender, EventArgs e) { opener.ValueFromSecondForm = valueFromForm2Textbox.Text; }
}
Other way could be to transfer the data at the time of closing the form2, here: Managing Data Among Multiple Forms (Part 1) [^]
Sandeep Mewara [My last tip/trick]: Browser back button issue after logout
-
Here you go! Pass value between forms using events[^]
Dave
Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum. Astonish us. Be exceptional. (Pete O'Hanlon)
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn) -
Here you go! Pass value between forms using events[^]
Dave
Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum. Astonish us. Be exceptional. (Pete O'Hanlon)
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)Cool! Nice link. 5!
Sandeep Mewara [My last tip/trick]: Browser back button issue after logout
-
How to transfer data between two form ? I found few articles but it is only transmitted in one direction instructions
there is three solution: 1、pipe. 2、shared memory. 3、Message.
-
:doh: Did you try?
//Form1:
private string valueFromSecondForm;public string ValueFromSecondForm
get { return value; }
set { valueFromSecondForm = value; }private void button1_Click(object sender, EventArgs e)
{
Form2 frm = new Form2(this);
frm.Show();
}//Form2:
public partial class Form2 : Form
{
Form opener;public Form2(Form parentForm) { InitializeComponent(); opener = parentForm; } private void button1\_Click(object sender, EventArgs e) { opener.ValueFromSecondForm = valueFromForm2Textbox.Text; }
}
Other way could be to transfer the data at the time of closing the form2, here: Managing Data Among Multiple Forms (Part 1) [^]
Sandeep Mewara [My last tip/trick]: Browser back button issue after logout
-
Here you go! Pass value between forms using events[^]
Dave
Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum. Astonish us. Be exceptional. (Pete O'Hanlon)
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn) -
there is three solution: 1、pipe. 2、shared memory. 3、Message.
You use anti-tank missles to kill mosquitoes don't you?
A guide to posting questions on CodeProject[^]
Dave Kreskowiak -
You use anti-tank missles to kill mosquitoes don't you?
A guide to posting questions on CodeProject[^]
Dave KreskowiakI would.
*pre-emptive celebratory nipple tassle jiggle* - Sean Ewington
"Mind bleach! Send me mind bleach!" - Nagy Vilmos
CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier
-
i want to transfer data between two form automatic two way, and response by click a button too. Sory because my English is not goog, i'm come from Viet Nam, i'm learning C# my self. This my first time post a Question. :)
Promance wrote:
want to transfer data between two form automatic two way, and response by click a button too.
Hi Promance, Then it sounds to me like you want ... at all times ... for the exact same content to appear on both forms ... yes ? That's "synchronization." And, if you have synchronization working, then why would you also need to have a button to press to make sure the content was synchronized ? Please describe more clearly the nature of the data you are synchronizing here: 1. is it only text characters: example: you want a textbox on Form1 to always display the same text as a textbox on Form2, and the reverse. 2. or, is the data "more than that" ? does the data include content which you interpret and then construct controls based on your interpretation ? And, finally, it would be helpful to clearly describe the source of the data: 1. by direct action of a user while your application is running: like typing in a text box 2. by making calls to some external database and getting new data ... or does some external source notify your application there is new data needed to be used ? Give us a brief description of what's on Form1 and Form2?: what kind of controls ? Custom controls from 3rd. party companies, Excel used via VSTO, a UserControl you created yourself, standard Visual Studio toolbox controls ? best, Bill good luck, Bill
"Humans are amphibians ... half spirit and half animal ... as spirits they belong to the eternal world, but as animals they inhabit time. This means that while their spirit can be directed to an eternal object, their bodies, passions, and imaginations are in continual change, for to be in time, means to change. Their nearest approach to constancy, therefore, is undulation: the repeated return to a level from which they repeatedly fall back, a series of troughs and peaks.” C.S. Lewis
-
Promance wrote:
want to transfer data between two form automatic two way, and response by click a button too.
Hi Promance, Then it sounds to me like you want ... at all times ... for the exact same content to appear on both forms ... yes ? That's "synchronization." And, if you have synchronization working, then why would you also need to have a button to press to make sure the content was synchronized ? Please describe more clearly the nature of the data you are synchronizing here: 1. is it only text characters: example: you want a textbox on Form1 to always display the same text as a textbox on Form2, and the reverse. 2. or, is the data "more than that" ? does the data include content which you interpret and then construct controls based on your interpretation ? And, finally, it would be helpful to clearly describe the source of the data: 1. by direct action of a user while your application is running: like typing in a text box 2. by making calls to some external database and getting new data ... or does some external source notify your application there is new data needed to be used ? Give us a brief description of what's on Form1 and Form2?: what kind of controls ? Custom controls from 3rd. party companies, Excel used via VSTO, a UserControl you created yourself, standard Visual Studio toolbox controls ? best, Bill good luck, Bill
"Humans are amphibians ... half spirit and half animal ... as spirits they belong to the eternal world, but as animals they inhabit time. This means that while their spirit can be directed to an eternal object, their bodies, passions, and imaginations are in continual change, for to be in time, means to change. Their nearest approach to constancy, therefore, is undulation: the repeated return to a level from which they repeatedly fall back, a series of troughs and peaks.” C.S. Lewis
It don't need complicated as you say. First, i want a textbox on Form1 to always display the same text as a textbox on Form2 when i press a letter key. Second, i have a project about File Explore in Windows, when i want to rename a folder or a file(i have used ListView to show file and folder), i must be add a form named Rename. And i'm stuck here, i can rename file, but can't update file's name in ListView or TreeView.