Execute code in form1 from form2 without creating new instance
Am trying to execute a command in form1 from form2 without creating a new instance. Is that possible
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
VulpesPosted Mar 2, 2015, 7:13 AM
Subject to that, all you need is a reference to your Form1 instance. There are many ways to get this but here's one which doesn't involve changes to your existing code:
// from Form2
Form1 f1 = Application.OpenForms["Form1"] as Form1;
if (f1 != null)
{
f1.SomeMethod();
}