3 Replies - 4091 Views - Last Post: 25 August 2010 - 04:19 AM Rate Topic: -----

#1 c#think#:)  Icon User is offline

  • D.I.C Head

Reputation: 17
  • View blog
  • Posts: 114
  • Joined: 17-March 10

Determine if a form is already opened...

Posted 18 August 2010 - 02:16 AM

Hi there Gurus!

I have App with MDI. MDI are menus that user chooses from the MainMenu.
For example: Menu "Options" has MenuItem "Groups". Clicking "Groups" will create an instance of class Groups type form.

My goal is to determine if the groups form is already open, that way user may avoid opening multiple Groups forms of one instance.

I have managed to achieve that myself, code follows:
private void miGroups_Clicked(object sender, EventArgs e)
        {
            bool isFormAlreadyOpen = false;            

            foreach (Form oForm in Application.OpenForms)
            {
                if (oForm is Groups)
                {
                    oForm.BringToFront();                    
                    oForm.WindowState = FormWindowState.Normal;
                    isFormAlreadyOpen = true;
                }
            }

            if (isFormAlreadyOpen == false)
            {            
                Groups grForm = new Groups();
                grForm.MdiParent = this;
                grForm.Show();
            }            
        }



it works fine, but... I have many MenuItems in many Menus and that makes me write the code above into each MenuItem Click event. It takes too much time and space.

What i want to do is to create a function that will check all currently opened forms for if there is already opened such instance. That way i will use this function in every Click event and will not write that code again.

What i have tryed so far:
bool isFOpenFlag;

private void isFormAlreadyOpen(Form oForm)
        {
            isFOpenFlag = false;
            
            foreach (Form f in Application.OpenForms)
            {
                if (f.Equals(oForm))
                {
                    oForm.BringToFront();
                    oForm.WindowState = FormWindowState.Normal;
                    isFOpenFlag = true;
                }
            }
        }

private void miGroups_Clicked(object sender, EventArgs e)
        {   
             isFormAlreadyOpen(Groups);
         
          if (isFormAlreadyOpen == false)
            {            
                Groups grForm = new Groups();
                grForm.MdiParent = this;
                grForm.Show();
            }            
        }



This code is incorrect, but i suppose it is very similar to right one. Due to very low experience in proggramming i do not know how to implement it.

Thanks in advance!

Is This A Good Question/Topic? 0
  • +

Replies To: Determine if a form is already opened...

#2 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1526
  • View blog
  • Posts: 5,961
  • Joined: 21-March 08

Re: Determine if a form is already opened...

Posted 18 August 2010 - 04:54 AM

you could try something like this...

private bool IsAlreadyOpen(Type formType)
{
    bool isOpen = false;

    foreach (Form f in Application.OpenForms)
    {
        if (f.GetType() == formType)
        {
            f.BringToFront();
            f.WindowState = FormWindowState.Normal;
            isOpen = true;
        }
    }

    return isOpen;
}



and you would use it like....

private void miGroups_Clicked(object sender, EventArgs e)
{   
    // this is assuming "Groups" is the name of the form
    //    you are wanting to open.
    bool isFormOpen = IsAlreadyOpen(typeof(Groups));
         
    if (isFormOpen == false)
    {            
         Groups grForm = new Groups();
         grForm.MdiParent = this;
         grForm.Show();
    }            
}


Was This Post Helpful? 1
  • +
  • -

#3 c#think#:)  Icon User is offline

  • D.I.C Head

Reputation: 17
  • View blog
  • Posts: 114
  • Joined: 17-March 10

Re: Determine if a form is already opened...

Posted 25 August 2010 - 02:04 AM

Hi eclipsed4utoo.

Thanks a lot!
This is exactly what i was looking for, a function that checks the form type passed by parameter, but i didn't know the syntax to implement it.

PS!
you have mistyped the IF clause
if(isFormAlreadyOpen == false) //it should be if(isFormOpne == false) because isFormAlreadyOpen is a function name :)/>
            {
                Groups grForm = new Groups();
                grForm.MdiParent = this;
                grForm.Show();
            }            



one more question:
u used bool isFormOpen = isFormAlreadyOpen(typeof(Groups)); to get bool state of the function and then in IF clause u check if it is true or false. I am not sure if understand what for u use it. Can't it be something like:
private void miGroups_Clicked(object sender, EventArgs e)
        {
            if(!(isFormAlreadyOpen(typeof(Groups))))
            {
                Groups grForm = new Groups();
                grForm.MdiParent = this;
                grForm.Show();
            }            



it takes less space :), or am missing something?

Thanks again!
C#Think#;)
Was This Post Helpful? 0
  • +
  • -

#4 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1526
  • View blog
  • Posts: 5,961
  • Joined: 21-March 08

Re: Determine if a form is already opened...

Posted 25 August 2010 - 04:19 AM

Yes, I did have a typo. Thanks for pointing it out.

Yes, you can do it like that. I found that it's easier for others to read when you break it apart. Easier for people who come to this forum to read and understand what is happening. That's the main reason I break them up so I don't have so many parenthesis.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1