How do I make my Master Form know when a secondary form has been closed, without using Parent and Child?

Asked

Viewed 705 times

3

I have my main form (image below), and in it a Split Container. I open a secondary form (which is currently displayed) on panel2 of SplitContainer, where the user selects an account to perform financial transactions.

As soon as he chooses the account, and click the button continuar of that form, if everything is OK, the form selection should be closed and, in its place, show another form with the available options.

How can I inform mine form main that the form selection has been closed, so it can load the next form?

I think about using events and delegate, is correct?

inserir a descrição da imagem aqui

1 answer

4


Yes, it would be correct to use events or delegates in that case. In the case of Winforms, I see no problem when putting a Handler of event at Parent pro OnClosed of the son:

private void button1_Click(object sender, EventArgs e)
{
    var newForm = new Form1();
    newForm.Closed += newForm_Closed; ;
    newForm.ShowDialog();
}

void newForm_Closed(object sender, EventArgs e)
{
    button1.Text = "Fechado";
}

Could also pass a delegate in the child form constructor and save the instance. Thus, when closing, simply call the delegate.

  • This is exactly what I wanted. I had forgotten how to do it, despite being very simple. Thanks!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.