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();
            }

Recommended Answers

All 3 Replies

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

I suggest you to use Application.OpenForms.

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

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();
            }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.