1
votes

In my application the user can open multiple forms.

Some forms are MDI, some not.

How can I detect, in a arbitrary (MDI or not) form if it's a active form or not?

from MSDN (Form.ActiveForm property explanation):

You can use this method to obtain a reference to the currently active form to perform actions on the form or its controls. If your application is a multiple-document interface (MDI) application, use the ActiveMdiChild property to obtain the currently active MDI child form.

I have 2 possibilities, one for MDI, other for non-MDI, but what if I have both forms open, how I detect which of them is really active?

1

1 Answers

0
votes

You can place this logic where you need to:

        if (Form.ActiveForm == null) return;

        if (Form.ActiveForm == this.MdiParent)
        {
            if (MdiParent.ActiveMdiChild != null) 
                Debug.WriteLine(MdiParent.ActiveMdiChild.Text);

            return;
        }

        Debug.WriteLine(Form.ActiveForm.Text);

Form.ActiveForm will return the non-MDI form that is currenly active. If the Active form is the MDI parent (main window), this will mean that an MDI form is active and so we look at MdiParent.ActiveMdiChild

This sample writes Form text to debug out but you can use the info in any way you wish.