Bora
Bora

Reputation: 149

how to change the property of the first form while using a second form?

I'm using two forms and I disable the first one when the second form shows up. I couldn't find a way to enable the first form when the second one is closed. Passing a parameter could be a solution but I bet there is a simpler way. First I thought of enabling the first form on the destructor of the second but could not do it. Anyone have any suggestions?

Upvotes: 1

Views: 186

Answers (2)

Servy
Servy

Reputation: 203802

As has been mentioned, in this specific case it probably makes sense to use a modal dialog for opening the second form.

To cover the case when that isn't applicable, the accepted best practice would be to subscribe to the FormClosing event of the second form from the first, and in the event handler you could enable "yourself" and do anything else that you might want to do as a result of the other form being closed. Here is a simple example:

public partial class ParentForm : Form
{
    private void button1_Click(object sender, EventArgs e)
    {
        ChildForm child = new ChildForm();

        child.FormClosing += new FormClosingEventHandler(child_FormClosing);
        Hide();
        child.Show();
    }

    private void child_FormClosing(object sender, FormClosingEventArgs e)
    {
        Show();
    }
}

Upvotes: 2

JleruOHeP
JleruOHeP

Reputation: 10376

You can show second form with ShowDialog() - form will be shown as modal, first form will be enabled only when second will be closed.

For future problems you can have a field in second form to have instance of first one, and use that instance, if you need, for example you can use custom constructor:

class SecondForm: Form
{
   FirstForm _parentForm;

   public SeconForm(FirstForm form)
   {
      InitializeComponent();
      _parentForm = form;
   }

   void DoSomethingWithParent()
   {
      _parentForm.DoSomesting();
   }
}

Upvotes: 2

Related Questions