I've been having problems figuring out a way to do this. I need it so that if the user opens form a and then later on buries that form under others, and they click the menu to open the form again that it just sets focus to it instead of opening another instance...

This is what I have now, is there a better, more elegant way of doing this or is this pretty much it?

//see if the form is already open, if it is, set focus to it, if not, open it
            foreach (Form a in MdiChildren)
            {
                if (a is frmEditorAccounts)
                {
                    //the form is open, set focus to it
                    a.BringToFront();
                    a.Activate();
                    break;
                }
            }
            if (ActiveMdiChild == null || ActiveMdiChild.Name != "frmEditorAccounts")
            {
                //no forms are open
                //open one
                frmEditorAccounts Accounts = new frmEditorAccounts();
                Accounts.MdiParent = this;
                Accounts.Show();
            }

>is there a better, more elegant way of doing this or is this pretty much it?

I suggest you to use Application.OpenForms.

Edited 6 Years Ago by __avd: n/a

Comments
good info, i wasnt familiar with OpenForms. You rpost prompted some reading and learning :)

To expand on what adatapost said, you can search the Application.OpenForms collection by name:

frmEditorAccounts Accounts = Application.OpenForms["frmEditorAccounts "] as frmEditorAccounts

Also, depending on how you have designed your forms, you may want to alter the WindowState of the form as well as bringing it to the front and activating it. If the form is minimised it wont appear when you activate it. Heres a rough idea:

frmEditorAccounts accounts = Application.OpenForms["frmEditorAccounts"] as frmEditorAccounts ;
            if (accounts != null)
            {
                accounts.WindowState = FormWindowState.Normal;
                accounts.BringToFront();
                accounts.Activate();
            }
            else
            {
                accounts = new frmEditorAccounts ();
                accounts.MdiParent = this;
                accounts.Show();
            }

Very nice! I think I'll use that! Thanks guys!

To expand on what adatapost said, you can search the Application.OpenForms collection by name:

frmEditorAccounts Accounts = Application.OpenForms["frmEditorAccounts "] as frmEditorAccounts

Also, depending on how you have designed your forms, you may want to alter the WindowState of the form as well as bringing it to the front and activating it. If the form is minimised it wont appear when you activate it. Heres a rough idea:

frmEditorAccounts accounts = Application.OpenForms["frmEditorAccounts"] as frmEditorAccounts ;
            if (accounts != null)
            {
                accounts.WindowState = FormWindowState.Normal;
                accounts.BringToFront();
                accounts.Activate();
            }
            else
            {
                accounts = new frmEditorAccounts ();
                accounts.MdiParent = this;
                accounts.Show();
            }
This question has already been answered. Start a new discussion instead.