consider i ve 2 window form.. f1,f2 say now from f1 i should call f2.showdialog().
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.
Hi,
Add this code in the relevant place in your from f1.
Form f2 = new Form();
f2.ShowDialog();
HTH
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)
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 u must make new constructor in the recieved form
and that wil be like that
public Form2(int x) { InitializeComponent(); this.x = x; }
and will be notice in the first form when u create the instance from form2 u have two choice on of them one of them let u to pass value
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.
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:
- Create an instance of the form to show
- Transfer data to that new form instance
- Show the form
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]
Hi, 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.
Hope this helps.
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
- FormToForm Using Parameters - http://windowsclient.net/learn/video.aspx?v=105861
HTH
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 } }