richard richard - 3 months ago 17
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

Just store it in a variable:

Form1 form = new Form1();
Application.Run(form);

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

private static Form1 form;

[STAThread]
static void Main()
{
    form = new Form1();
    Application.Run(form);
}

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...