Skip to content

Calling functions on the default form

Ian Atkinson

Asked Dec 8, 2005, 1:13 AM EST1 answer
Share
LinkedInX
Hi, I'm sure this is a simple question but I can't for the life of me remember how to do it! Say I have my main form of type Form1, and from that form I create another form of type Form2, how do I call public functions in Form1 from Form2? The problem is that I don't know what the instance of Form1 is called since it's created when the application starts. Normally if I create another form I set the name, for example: Form2 foo = new Form2(); foo.publicFunction(bar); However with this scenario as I say I don't know how to refer to the default form! Many thanks, Ian Atkinson

1 Answer

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

  • 0
    I've managed to fix this now, I'll post the solution so it's archived. In the Program.cs file which launches the first form the default line reads as follows: Application.Run(new Form1()); What I did was set a static variable at the top of the Program class like this: public static Form1 mainForm; and then changed the run line to be as follows: Application.Run(mainForm); Functions on this form can then be called by referring to mainForm, for example: Program.mainForm.publicFunction(); I'm sure there are neater ways (and I'd love to hear them) to do this but this does work! Ian.
    0