Calling function in one form from another
I have a program where Form1 is a MDI parent to form 2. In the menu on Form1, I have an option which creates a dialog (Form3) for the user to enter information and save that information to a file. When the user clicks the button on Form3 to save the information, I would like to be able to do one of two things. Add this information to a List on Form2 or call a function on Form2 to reload all the data from a file. I have not been able to figure out how to do either.
Another option I have considered, but do not know how to do is create an event handler which would fire when Form2 regains focus, I have not found an event which I feels works for this, but I am guessing I have just missed it. This option is behind the other options, as I would reload the data more often than should really be required, but would work if the above will not.
oupoiPosted Aug 28, 2006, 9:08 PM
The simplest one is pass the object instance of Form2 to Form3 through Form1. e.g.
/*******Form1.cs********/ //Create Form2
Form2 f2 = new Form2();
f2.MdiParent = this;
f2.Show();
//Create Form3
Form3 f3 = new Form3();
f3.formToCallback = f2; //"formToCallback " should be a public variable of Form2 type in Form3
f3.ShowDialog(this); /*******Form3.cs********/
formToCallback.myFunc(); //"myFunc" should be the public function in Form2 that you are willing to call
RussPosted Aug 28, 2006, 8:23 PM
jc mandapatPosted Aug 28, 2006, 11:12 AM