C# Form Referencing
Sample code for System.Windows.Forms.Form class referencing.
Parent Form:
namespace ExampleProgram
{
public partial class Form1 : Form
{
public int ChangeThisNumber = 0;
public Form1()
{
InitializeComponent();
Form child = new Form(this);
child.Show();
}
public void RunThisMethod()
{
ChangeThisNumber = 2;
}
}
}
Child Form:
namespace ExampleProgram
{
public partial class Form2 : Form
{
Form1 ParentForm;
public Configuration(Form1 f)
{
InitializeComponent();
ParentForm = f;
// Change the public variable:
ParentForm.ChangeThisNumber = 1;
// Run the public method:
ParentForm.RunThisMethod();
}
}
}