I have a windows form named MainForm.cs and two user controls which are UserControlRed.cs and UserControlBlue.cs respectively. An object of UserControlRed that is ucr is created and added into panel1 in MainForm by using panel1.Controls.Add(ucr); in default constructor of MainForm.
Then I have the UI like this: MainForm

Note: instance of UserControlBlue that is ucb is created using the click button event in UserControlRed. Code as follows:
private void CreateUCBbtn_Click(object sender, EventArgs e)
{
MainForm f1 = new MainForm();
f1 = (MainForm)this.Parent.Parent;
f1.AddUCBlueIntoPanel2();
}
Now, what I want to do is after user click the ‘Change UCB Label’ button, the label in UserControlBlue will change accordingly to what has been entered in textbox in UserControlRed. So, I have two questions here:
- Regarding my Note above, is it safe to do it like this?
- What is the best way for me to pass the value in the textbox from UserControlRed into the label in UserControlBlue?
Another note: I’m still new in OOP and especially C# and winforms. Maybe anyone can suggest me a good site to familiar with?
You can add a public method (function) in UserControlBlue that accepts a string as a parameter and assigns it to the label’s text. Then you can call that method from the “Change UCB Label” button click handler.
It should be safe to do this.