ucA should raise an event that it's host control (Form?) subscribes to. ucB should have a public property who's setter sets the Label's Text.
The host control when instanciating ucB should maintain a private reference to it, when it handles the event raised in ucA it can then set the property of ucB.
Have a look at my
Events Made Simple[
^] article.
Edit: What the heck, I'm in a good mood this morning - here's a full working (WinForms) example.
using System.Drawing;
using System.Windows.Forms;
namespace UserControlInteraction
{
public partial class Form1 : Form
{
private UCA ucA;
private UCB ucB;
public Form1()
{
InitializeComponent();
ucA = new UCA();
ucA.Size = new Size(150, 50);
ucB = new UCB();
ucB.Location = new Point(0, 50);
Controls.AddRange(new Control[] { ucA, ucB });
ucA.UpdateText += new EventHandler<TextEventArgs>(ucA_UpdateText);
}
void ucA_UpdateText(object sender, TextEventArgs e)
{
ucB.LabelText = e.Text;
}
}
public class UCA : UserControl
{
public event EventHandler<TextEventArgs> UpdateText;
private TextBox textBox;
private Button button;
public UCA()
{
textBox = new TextBox();
textBox.Location = new Point(0, 0);
button = new Button();
button.Location = new Point(0, 24);
button.Text = "&Update";
button.Click += new EventHandler(button_Click);
Controls.AddRange(new Control[] { textBox, button });
}
private void button_Click(object sender, EventArgs e)
{
OnUpdateText(new TextEventArgs(textBox.Text));
}
protected virtual void OnUpdateText(TextEventArgs e)
{
EventHandler<TextEventArgs> eh = UpdateText;
if (eh != null)
eh(this, e);
}
}
public class UCB : UserControl
{
private Label label;
public UCB()
{
label = new Label();
label.Size = new Size(100, 13);
Controls.Add(label);
}
public string LabelText
{
get { return label.Text; }
set { label.Text = value; }
}
}
public class TextEventArgs : EventArgs
{
private string text;
public TextEventArgs(string text)
{
this.text = text;
}
public string Text
{
get { return text; }
}
}
}