richard richard - 1 year ago 104
C# Question

How can a reference to the form created by Application.Run(new Form1()) be obtained by the Main method in the static Program class?

I want to get a screen capture on the form created by

Application.Run(new Form1());
. But to do this I need to get a reference to that form.

How can this be done?

Answer Source

Just store it in a variable:

Form1 form = new Form1();

or if you need it in a static variable instead of a local one:

private static Form1 form;

static void Main()
    form = new Form1();

There's nothing magical about Application.Run(new Form1()); - it's still just creating an instance of Form1 and passing the reference to the Run method...

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download