Windows Application Form

I have a user control called 'ucForm', this 'ucForm' have a button to pop out a windows called 'pForm', I want to code it like this, when I press the 'Next' button it will close the 'pForm' and pass the textbox value in 'pForm' to 'ucForm' textbox. What code should I do in the button_click in 'pForm' and what event and method should used to detect the closing of the 'pForm' and perform passing value. Thanks

Note: I am using .net framework 2.0 and form_closing is oboslete in that.
Source: http://msdn.microsoft.com/en-us/library/system.windows.forms.form.closing.aspx

Dani AI

Generated

Two practical, maintainable approaches work better than polling IsDisposed: show the pop-up as a modal dialog and read a property or DialogResult when it returns (what hinted at), or have the pop-up raise a small event with the chosen value so the host uC receives it immediately. The Dispose-only approach you used, , will work in simple cases but is brittle (lifecycle/race issues, harder to follow and test).

Modal (ShowDialog + DialogResult or public property)

using(var dlg = new pForm())
{
    if (dlg.ShowDialog(this) == DialogResult.OK)
    {
        ucTextBox.Text = dlg.ResultText;   // pForm exposes ResultText
    }
}

On the pop-up (pForm) set the value and DialogResult before closing:

private void btnNext_Click(object sender, EventArgs e)
{
    this.ResultText = txtBox1.Text;
    this.DialogResult = DialogResult.OK;   // causes ShowDialog to return
}

ShowDialog blocks and returns a DialogResult you can test; when used as a modal dialog you should Dispose the form when you’re done. (learn.microsoft.com)

Event/delegate (modeless or to push value immediately)

public event EventHandler<TextSelectedEventArgs> TextSelected;

protected virtual void OnTextSelected(string text) =>
    TextSelected?.Invoke(this, new TextSelectedEventArgs(text));

private void btnNext_Click(object sender, EventArgs e)
{
    OnTextSelected(txtBox1.Text);
    this.Close();
}

Subscribe from the host and update the control in the handler. This follows standard .NET event patterns and keeps responsibilities clear. (learn.microsoft.com)

Few practical tips: prefer FormClosing/FormClosed over the old Closing/Closed events (those were deprecated). Don’t rely on polling IsDisposed to learn that a form closed—use DialogResult, FormClosed, or a dedicated event. If you use ShowDialog, dispose the dialog instance when finished to release resources. (learn.microsoft.com)

Recommended Answers

All 2 Replies

If you use the ShowDialog method, the execution stops until the form is closed.
Take a look at this thread.

What I actually did is

private void btnContinue_Click(object sender, EventArgs e)
{
    this.Dispose();
}

and on my ucForm I check if the pForm.IsDispose() then pass the parameter.

This actually works for my case.
Thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.