I have a problem concerning delegates in a Windows Forms application.
There are two forms:
-
the main form, which has a button named "Settings".
-
the "settings" form, this is the "child" form.
When I click the "Settings" button in the main form, it opens an instance of the Settings form.
My problem is that I need to pass a variable to the Settings form, when I open it. So that the new form will show the variable text. I don’t know how to retrieve the information in the child "Settings" form. I did this by following a tutorial online and could not understand from the tutorial how to read the information in the destination form.
Here’s what I’ve done so far, the code in the main form:
public partial class MainForm : Form
{
/// <summary>
/// Delegate to send data between forms
/// </summary>
public delegate void PageInfoHandler(object sender, PageInfoEventArgs e);
/// <summary>
/// Event of the delegate
/// </summary>
public event PageInfoHandler PageInfoRetrieved;
// Other stuff, events, blah, blah
private void toolStripBtnSettings_Click(object sender, EventArgs e)
{
PageInfoEventArgs args = new PageInfoEventArgs(SomeString);
this.OnPageInfoRetrieved(args);
SettingsForm settingsForm = new SettingsForm();
settingsForm.ShowDialog();
}
private void OnPageInfoRetrieved(PageInfoEventArgs args)
{
if (PageInfoRetrieved != null)
PageInfoRetrieved(this, args);
}
}
Pass any information you want to in to the constructor of Settings form, and provide accessor methods for things you need out of there.
In the above “example” you can construct a SettingForm with a string and get at an integer it may use. In your code: