I’d like to pass my form as a reference into a class, so the class object can access public methods from the form. I’ve tried it in a few different places, but each one has a few limitations.
Is there a way to instantiate the class from outside of the events, but still pass in the form?
namespace MyApplication
{
public partial class Form1 : Form
{
//If I instantiate the class here, the form seemingly doesn't exist yet
//and can't be passed in using "this."
Downloader downloader = new Downloader(this);
private void Form1_Load(object sender, EventArgs e)
{
//If I instantiate the class here, the form can be passed in, but the
//class object can't be seen outside of this event.
Downloader downloader = new Downloader(this);
}
private void downloadButton_Click(object sender, EventArgs e)
{
//If I instantiate the class here, the form can be passed in, but the
//class object can't be seen outside of this event.
Downloader downloader = new Downloader(this);
downloader.dostuff();
}
}
}
You are almost there. Change it to: