To simply describe what you are doing in VB: When a Button is Clicked on an instance of Form2:
1. take a pre-existing instance of Form3
2. insert that instance of Form3 into a Panel Control in an instance of Form1
It is not a good idea to put a Form inside another Form, or inside a Control in another Form (unless you are using the old MDI architecture, which I suggest you avoid). OriginalGriff is right-on when he advises you to use a UserControl !
To help you get a better understanding of programming in WinForms, we need to know which Form is the "main" Form here: does Form1 create Form2, and Form3 ?
If that's the case, then we can say your goal is:
1. to give an instance of Form2 a way to communicate to Form1 that it needs to insert an instance of a UI into a Panel inside it.
So, we give Form1 a way to subscribe to the Button Click Event on Form2 like this:
In Form2: by creating a publicly acessible reference to its Button:
public partial class Form2 : Form
{
public Button f2Button { set; get; }
public Form2()
{
InitializeComponent();
f2Button = this.button1;
}
}
In Form1: by subscribing to the Button's Click Event
public partial class Form2 : Form
{
public Form1()
{
InitializeComponent();
}
Form2 f2 = new Form2();
private void Form1_Load(object sender, EventArgs e)
{
f2.f2Button.Click += f2Button_Click;
}
private void f2Button_Click(object sender, EventArgs e)
{
MyUserControl uc = new MyUserControl;
panel1.Controls.Add(uc);
}
}
By constraining what Form2 exposes to other Forms/Classes to only its Button, we are trying to keep the "coupling" (dependency) between Forms/Classes to a minimum.