C# : Sending data from one form to other using delegates : Part 27
- Select the form whom you wish to send the data, who all are the subscribers to the notification.
- This will make you to decide the type of parameter that you wish to send and to be used in delegates.
- Then it is necessary to decide which form will be sending notification, or it can be also said that who is the owner.
Steps
- Declare the delegate
- Create the variable of delegate
- When you initialize the form point/assign the method to the delegate
- Call the delegate when required and pass the parameter.
Main Form / Owner Form code
namespace Delegates
{
public partial class MainForm : Form
{
public delegate void SetTextValueCallback(string str);
public SetTextValueCallback SetTextValue;
public MainForm()
{
InitializeComponent();
}
private void MainForm_Load(object sender, EventArgs e)
{
}
private void btnShowForm_Click(object sender, EventArgs e)
{
ChildForm frm = new ChildForm();
this.SetTextValue += new SetTextValueCallback(frm.SetText);
frm.Show();
}
private void btnAppend_Click(object sender, EventArgs e)
{
SetTextValue(txtEnterText.Text);
}
}
}
namespace Delegates
{
public partial class ChildForm : Form
{
public ChildForm()
{
InitializeComponent();
}
private void ChildForm_Load(object sender, EventArgs e)
{
}
public void SetText(string str)
{
lblAppendedText.Text = str;
}
}
}
No comments