Reputation: 6117
Consider the situation where I have two windows forms, say F1
and F2
. After using F1
, I have now called F2.ShowDialog()
. This puts F2
on the screen as well. Now that both forms are visible, how can I pass data from F1
to F2
? Additionally, once F2
(A modal dialog) finishes, how can I return data to F1
?
Upvotes: 7
Views: 26142
Reputation:
int x=10;
Form1 f2 = new Form1(x);
f2.ShowDialog();
This is wrote in the form who will pass the value. To recive this value you must make new constructor in the recieved form
and that will be like that
public Form2(int x)
{
InitializeComponent();
this.x = x;
}
and will be notice in the first form when you create the instance from form2 you have two choice on of them one of them let you to pass value
Upvotes: 3
Reputation: 7679
May be I am late. but all those who may want.
In destination form have a constructor defined like this
public partial class Destination: Form
{
string valueAccepted;
public Destination(string _valuePassed)
{
InitializeComponent();
this.valueAccepted= _valuePassed;
}
}
and in Source Form call the form like this
Source sourceForm= new Source ("value Passed");
sourceForm.ShowDialog();
this way "value Passed" is passed from Form Source to Form Destination
Upvotes: 0
Reputation: 165
There are multiple ways to pass data between 2 forms check these links which has example videos to do this
-FormToForm Using Properties - http://windowsclient.net/learn/video.aspx?v=108089
Upvotes: -1
Reputation: 2880
Use a defined Type for your information (class, struct...) and declare a variable of that in Form1
struct myData
{
String str1;
String str2;
}
Public Class Form1
{
Public myData dat;
}
(Note: the type shouldnt really be public, this is just for the sake of this example) Thus the data sits within Form1. Modify the constructor of Form2 to accept a parameter of type Form1.
public Form2(Form1 frm1)
{
mFrm1 = frm1;
InitializeComponent();
}
Now, when you call form2, send in the very instance of Form1 that's making the call:
Form2 frm2 = new Form2(this);
frm2.ShowDialog();
Now, when execution has reached Form2, you can access the MyData within Form1:
mFrm1.dat;
In this way, both instances of Form1 and Form2 will be referencing the data that's in one place. Making changes/updates will be available for both instances of the forms.
Upvotes: 0
Reputation: 31
Has anyone considered simply passing the value to the form in the tag attribute.
Form newForm = new form();
newForm.Tag = passValue;
newform.showmodal();
when newform is shown the load (or any other) routine can use the data in tag
public void load()
{
if (this.Tag.length > 0)
{
// do something with the data
}
}
Upvotes: 3
Reputation: 158389
Let's say you have a TextBox control in Form1, and you wish to pass the value from it to Form2 and show Form2 modally.
In Form1:
private void ShowForm2()
{
string value = TheTextBox.Text;
Form2 newForm = new Form2();
newForm.TheValue = value;
newForm.ShowDialog();
}
In Form2:
private string _theValue;
public string TheValue
{
get
{
return _theValue;
}
set
{
_theValue = value;
// do something with _theValue so that it
// appears in the UI
}
}
This is a very simple approach, and might not be the best for a larger application (in which case you would want to study the MVC pattern or similar). The key point is that you do things in the following order:
When you show a form modally it will block the code in the calling form until the new form is closed, so you can't have code there that will transfer information to the new form in a simple way (it's possible, but unnecessarily complicated).
[edit: extended the property methods in Form2 to be more clear]
Upvotes: 1
Reputation: 36987
Consider to use the MVC pattern, i.e. instead of having too much data in forms and passing them around, use model classes that keep to keep the stuff.
Upvotes: 0
Reputation:
let me reframe the question i ve 2 form f1, f2... i cal Form f2 = new Form(); f2.ShowDialog();
// now i need to pass the parameter from f1 window to f2 (which is modal dialog box) also return value from f2 form to f1
//now 'm using varibles that are in common namespace (for both f1 , f2)
Upvotes: 0
Reputation: 14913
Add this code in the relevant place in your from f1.
Form f2 = new Form();
f2.ShowDialog();
HTH
Upvotes: 2
Reputation: 17775
If you just want to push data to your child dialog, consider adding parameters to the child dialog's constructor, then calling ShowDialog(). Passing data the other way, though, is a little trickier.
Upvotes: 0