1

I have a MDI child (FormA) form that opens and displays a dataGridView.

The user has the ability to make changes to a record in the database by opening another form (FormB) that is outside of the MDI. Meaning that it is called with FormB.ShowDialog() rather than FormB.Show().

FormA has an activated event that I want to be thrown when the user returns from FormB. I do this to refresh the datagridview's datasource.

It seems that FormA never loses focus and I need to in order for the activated event to be thrown when the user returns. Is this by design? How can I achieve this functionality?

EDIT

FormB is called from FormA's MDI Parent. not FormA itself.

3
  • I believe that because it is a ShowDialog call, the original form is not really losing focus per se. Instead why don't you fire the event when the dialog returns? if (FormB.ShowDialog() == DialogResult.OK) { refreshTheDataSource(); }
    – Evan L
    Mar 26, 2014 at 17:04
  • @EvanL I would do that if FormB was being called from FormA but its not. Its being called from the MDI Parent
    – Nate S.
    Mar 26, 2014 at 17:08
  • 1
    An MDI child form's Activated event only runs when you switch from one child to another. Something you'll have to deal with. Mar 26, 2014 at 17:19

1 Answer 1

2

Why do you need Activated event at all? The FormB.ShowDialog() opens FormB in a modal state, so execution of the code in the calling form FormA is interrupted.
See the sample (to use in the FormA):

using (FormB frm = new FormB())
    if (frm.ShowDialog(this) == DialogResult.OK)
    {
        // here FormB is closed by user.
    }

EDIT
Since FormB is opened from the MDI parent, you can save the currently active form to variable and then pass the execution to it. Try this in the MDI parent form:

FormA saveForm = ActiveMdiChild as FormA;
using (FormB frm = new FormB())
    if (frm.ShowDialog(this) == DialogResult.OK)
    {
        if (saveForm != null)
            saveForm.UpdateDataGridView();  // Call the method of a previously active form.
    }

You can also try the ActivateMdiChild method to activate a child form.

3
  • FormB isn't being called from FormA. Its being called from the MDI Parent
    – Nate S.
    Mar 26, 2014 at 17:06
  • 1
    The saveForm sees only accessible members of the FormA class: having public, and maybe internal, protected internal access modifiers. So set public modifier for the desired methods.
    – Dmitry
    Mar 26, 2014 at 18:25
  • There are a lot of ways I can use this. Thank you!!
    – Nate S.
    Mar 26, 2014 at 18:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.