Solved

c# check if mdi form is open and then active it if it is, partial code returning errors

Posted on 2011-03-16
3
909 Views
Last Modified: 2013-12-17
hi all,

im trying to check if a form is open on click and if it is go to that tab instead of creating a new one
my code is below and it returns this error on click

"Collection was modified; enumeration operation may not execute."
private void iHome_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            FormCollection fc = Application.OpenForms;

            foreach (Form frm in fc)
            {
                if (frm.Name == "frmHome")
                {
                    frm.Activate();
                    return;
                }

                Athena.frmHome Home = new Athena.frmHome();
                Home.MdiParent = this;
                Home.Show();
            }

        }

Open in new window

0
Comment
Question by:awilderbeast
  • 2
3 Comments
 
LVL 44

Accepted Solution

by:
AndyAinscow earned 500 total points
ID: 35147359
private void iHome_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            FormCollection fc = Application.OpenForms;

            foreach (Form frm in fc)
            {
                if (frm.Name == "frmHome")
                {
                    frm.Activate();
                    return;
                }
            }      ///   ***************  Note this was after the next 3 statements before

                Athena.frmHome Home = new Athena.frmHome();
                Home.MdiParent = this;
                Home.Show();
        }
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 35147368
Your original code only checked the first form then added a new form is the first didn't meet your requirements.  I assume this was not what you wanted.
0
 
LVL 1

Author Closing Comment

by:awilderbeast
ID: 35147375
THANKS! :D
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

816 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now