Skip to content

Calling function in one form from another

Russ

Asked Aug 28, 2006, 6:07 AM EDT3 answers
Share
LinkedInX
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.

3 Answers

Know the answer? Post it. Somebody with the same question will find it here.

  • 0
    I think when you want to call a function in form2, you just have to call first the form and then call the function. EX: Form2.RetrieveFile() hope this helps
    0
  • 0
    That was one of my guesses too. Unfortunitly, it did not work out.
    0
  • 0
    There are many ways to do.
    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

    0