So i've a Form1 with three UserControl's as UC1,UC2 & UC3. Form1 contains a Button, UC1 and UC3. Both UC1 & UC3 are initially hidden when the application runs and the Form1 button click event makes the UC1 visible. UC1 contains a panel and inside the UC1 load event I'm adding UC2 into the panel through panel.Controls.Add() method at application run time. UC2 contains a Button and the click event of this button makes UC3 visible in Form1. UC3 contain a textbox. Now when I click the UC2 Button, I want it to pass a string value to UC3 textbox. How do I do that? or generally I need a simple solution to send values from one UserControl to another UserControl.
Here is all my code...
Form1.cs
namespace Problem
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
uC11.u3Visible += UC11_u3Visible;
}
private void UC11_u3Visible(object sender, EventArgs e)
{
uC31.Visible = true;
uC11.Visible = false;
}
private void button1_Click(object sender, EventArgs e)
{
uC11.Visible = true;
}
}
}
UC1.cs
namespace Problem
{
public partial class UC1 : UserControl
{
public event EventHandler u3Visible;
public UC1()
{
InitializeComponent();
}
private void UC1_Load(object sender, EventArgs e)
{
UC2 xy = new UC2();
xy.Name = "UsrCntrl2";
xy.Location = new Point(18, panel1.Controls.Count * 73);
panel1.Controls.Add(xy);
xy.open += Xy_open;
}
private void Xy_open(object sender, EventArgs e)
{
EventHandler handler = u3Visible;
if (handler != null)
{
handler(this, new EventArgs());
}
}
}
}
UC2.cs
namespace Problem
{
public partial class UC2 : UserControl
{
public event EventHandler open;
public UC2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
EventHandler handler = open;
if (handler != null)
{
handler(this, new EventArgs());
}
string text = "Hello There!";
var ass = new UC3(text);
}
}
}
UC3.cs
namespace Problem
{
public partial class UC3 : UserControl
{
public UC3()
{
InitializeComponent();
}
public UC3(string text)
{
InitializeComponent();
TB.Text = text;
MessageBox.Show(text);
}
}
}
What I have tried:
I'm stuck with this problem for so many hours now. I have tried googling to finds related solutions but nothing that much helpful came-up. So had to post it here. Any help will be greatly appreciated. Thanks in advance!