Click here to Skip to main content
15,893,622 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi

i want to how to pass value from child from to parent form
for example

im in form1

form2 a = new form2()
a.showDialog(this)////this is parent form ((form1))


i want to return value form form2 to form1 when i finish from form2

if any one can help me please as fast as you can

thanks
Posted

This is way too simple. Wrap show dialog. Remember, when ShowDialog closes the form, it still exists. Also, don't close it, just hide.

C#
partial class MyDialogForm : Form {
   internal MyData ControlsToData() { /* implement getting value you need */ }
   protected override void OnFormClosing(FormClosingEventArgs e) {
       if (e.CloseReason == CloseReason.UserClosing) {
           e.Cancel = true;
           Hide();
       } //if user closing
   } //OnFormClosing
} //class MyDialogForm

//...

MyDialogForm MyDialog = new MyDialogForm();

//...

MyData ShowDialog() {
    MyDialog.ShowDialog();
    return MyDialog.ControlsToData();
} //ShowDialog

//or if you want to take into account <code>DialogResult</code>, can be something like that:
MyData ShowDialogWithCancel() {
    if (MyDialog.ShowDialog() == DialogResult.Cancel)
        return null;
    return MyDialog.ControlsToData();
} //ShowDialogWithCancel


—SA
 
Share this answer
 
v2
if you pass the parent to the child when you create it, i.e.

//in FormA...
//call to instantiate formB
formB b = new formB(this); in the parent form
b.Show();
//to do that you need to create a new constructor and add a variable in formB similar to what's shown below

//create variable to hold any form object
private System.Windows.Forms.Form thisParent;
//new construction
public formB(Form f)
{
this.thisParent = f;
InitializeComponent();
}

//then
//create a public function in formA that handles the value you want to pass to formA
public void DoSomething(string someValue)
{
textbox1.text = somevalue;
return;
}


//In the formB.cs add something like the following
//to pass the value to formA
formA a = (formA)this.thisParent;
a.DoSomething("valueToPass");

This also give you the option to close formB and redisplay formA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900