A set of .NET Framework managed libraries for developing graphical user interfaces.
The following is conceptual which can be plugged into your code with of course modifications.
Here in the child form, in this case we want a value in a TextBox and only close the form if the TextBox has a value.
We setup a delegate/event in the child form.
public delegate void OnPassInformation(string sender);
public event OnPassInformation PassInformation;
Button code
private void SubmitButton_Click(object sender, EventArgs e)
{
if (!string.IsNullOrWhiteSpace(FirstNameTextBox.Text))
{
PassInformation?.Invoke(FirstNameTextBox.Text);
DialogResult = DialogResult.OK;
}
else
{
MessageBox.Show("Need a first name");
}
}
Then
The following is in a button click event, would be done in your current code.
private void DisplayButton_Click(object sender, EventArgs e)
{
using (ChildForm form = new ChildForm())
{
form.PassInformation += delegate(string s)
{
Debug.WriteLine(s);
};
form.ShowDialog(this);
}
}
The benefits are, no need to access a form control and should be easy to following in the debugger with breakpoints.