K
K
kykyryky2016-01-31 15:22:06
Programming
kykyryky, 2016-01-31 15:22:06

How to intercept the closing of the child form in the parent form?

The essence of the problem on a simple example. There is a database of a car repair shop with three tables: customers, their cars and orders. There are two forms: parent and child. The parent form is an ordering window with a bunch of textboxes that contain the data of the owner, his car, etc. If the client is not new, it is possible to open a child form with a table of clients, in which you can select the desired row, and after double-clicking on it, the program will scatter the data over textboxes, and the form will close.
And now the question itself: after closing the child form, it would be necessary to check if there is a car belonging to the customer in the database and, if it exists, report this in the form of a MessageBox, load data about it into the form. Or offer to choose a car if the customer has several of them. And for this, in the parent form, it is necessary to determine that the child form has just been closed.
Of the possible solutions, I still see only catching the Activated events on the form, despite the fact that it will fire not only after the child form is closed, somehow handle this particular case, and ... perhaps that's all.
What other options are there?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Nemiro, 2016-01-31
@kykyryky

Just add the form close event handler:

var f = new Form2();
f.Owner = this;

f.FormClosed += (object s, FormClosedEventArgs args) =>
{
  MessageBox.Show("Форма закрыта!");
};

f.Show();

Either like this:
private void button1_Click(object sender, EventArgs e)
{
  var f = new Form2();
  f.Owner = this;

  f.FormClosed += Form2_FormClosed;

  f.Show();
}

private void Form2_FormClosed(object sender, FormClosedEventArgs e)
{
  MessageBox.Show("Форма закрыта!");
}

You can also show the child form as a dialog (not suitable for an Mdi container ):
var f = new Form2();
f.Owner = this;
// главная форма будет недоступна
f.ShowDialog();
// после закрытия диалоговой формы, выполнение продолжится
MessageBox.Show("Форма закрыта!");

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question