Witam,
UserControl zawiera 2 pola TextBox (txtSource, txtDestination) i Button (btnSend):
namespace WinFormsUserControlSample
{
using System;
using System.Windows.Forms;
public partial class PanelUserControl : UserControl
{
public PanelUserControl()
{
this.InitializeComponent();
}
public delegate void ChangeSourceDelegate(StringEventArgs e);
public ChangeSourceDelegate ChangeSourceDelegateInstance;
public string Destination
{
set { this.txtDestination.Text = value; }
}
private void btnSend_Click(object sender, EventArgs e)
{
if (ChangeSourceDelegateInstance != null)
{
ChangeSourceDelegateInstance(new StringEventArgs(this.txtSource.Text));
}
}
}
}
teraz twoja główna formatka (MainForm) zawiera dwie instancje UserControl (LeftPanel, RightPanel):
namespace WinFormsUserControlSample
{
using System.Windows.Forms;
public partial class MainForm : Form
{
public MainForm()
{
this.InitializeComponent();
this.leftPanel.ChangeSourceDelegateInstance += e =>
{
this.rightPanel.Destination = e.Value;
};
this.rightPanel.ChangeSourceDelegateInstance += e =>
{
this.leftPanel.Destination = e.Value;
};
}
}
}
Teraz gdy zmienię zawartość pola tekstowego oznaczonego jako Source w pierwszej instancji UsetControl i kliknę przycisk Send zawartość pola Destination w drugiej instancji UserControl zmieni się. Druga instancja UserControl działa analogicznie
Dane przesyłane są zewnętrzną klasą
namespace WinFormsUserControlSample
{
using System;
public sealed class StringEventArgs : EventArgs
{
public string Value { get; private set; }
public StringEventArgs(string value)
{
this.Value = value;
}
}
}
Pozdrawiam,
mr-owl