Here is my approach:
More logical and more simpler I think.
Just hide and unhide the usercontrol, and transfer data between through public variables.
public partial class UserControl1 : UserControl
{
public UserControl1()
{
InitializeComponent();
}
private void label1_Click(object sender, EventArgs e)
{
this.Visible = false;
}
public string data = "";
private void button1_Click(object sender, EventArgs e)
{
data = "crystal";
Close();
}
private void button2_Click(object sender, EventArgs e)
{
data = "gas";
Close();
}
private void button3_Click(object sender, EventArgs e)
{
data = "cell";
Close();
}
private void pictureBox1_Click(object sender, EventArgs e)
{
controlClicked = "pictureBox1";
userControl11.Visible = true;
}
string mainData = "";
string controlClicked = "";
private void userControl11_VisibleChanged(object sender, EventArgs e)
{
if (userControl11.Visible == false )
{
if (userControl11.data == "crystal")
{
mainData = "crystal";
if (controlClicked == "pictureBox1")
{
pictureBox1.BackgroundImage = Image.FromFile("1.png");
}
}
if (userControl11.data == "gas")
{
mainData = "gas";
}
if (userControl11.data == "cell")
{
mainData = "cell";
}
}
}