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
1 Answer
Know the answer? Post it. Somebody with the same question will find it here.
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
VulpesRank #55 · 29.2K reputationPosted Mar 2, 2015, 2:13 AM EST
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();
}