Click here to Skip to main content
15,891,409 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
The problem is that when i call a method named as avoid_multiple_forms() (currently its been commented)for different click events it only gets effective for just one tool-strip click event and the other conditions in the method won't work and gives wrong output . Now the thing is that i am not getting, whether the method is overwriting the previous condition or what ? Any solutions for it? Please also refer the code i tried.

What I have tried:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq; 
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Form2
{
    public partial class Form4 : Form
    {
        public Form4()
        {
            InitializeComponent();
        }

      /*  void avoid_multiple_forms(Form frm)
        {
            bool open = false,open1=false ,open2=false;
            foreach (Form OpenForm in Application.OpenForms)
            {
                if (OpenForm.GetType() == typeof(Form1))
                {
                    OpenForm.BringToFront();
                    open = true;
                }
               
                if (OpenForm.GetType() == typeof(Form2))
                {
                    OpenForm.BringToFront();
                    open1 = true;
                }
                if (OpenForm.GetType() == typeof(Form3))
                {
                    OpenForm.BringToFront();
                    open2 = true;
                }
            }
            if (open == false)
            {
                frm.MdiParent = this;
                frm.Show();
                frm.AutoScroll = true;
               
            }
            if (open1 == false)
            {
                frm.MdiParent = this;
                frm.Show();
                frm.AutoScroll = true;
               
            }
            
            if (open2 == false)
            {
                frm.MdiParent = this;
                frm.Show();
                frm.AutoScroll = true;
            }
            
        }*/
      
        private void newAccountRegistrationToolStripMenuItem_Click(object sender, EventArgs e)
        {
             Form1 frm1 = new Form1();
            // avoid_multiple_forms(frm1);
            bool open = false;
            foreach (Form OpenForm in Application.OpenForms)
            {
                if (OpenForm.GetType() == typeof(Form1))
                {
                    OpenForm.Activate();
                    open = true;
                }
            }
            if (open == false)
            {
                frm1.MdiParent = this;
                frm1.Show();
                frm1.AutoScroll = true;
            }
            


        }

        private void performTransactionsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form2 frm2 = new Form2();

            // avoid_multiple_forms(frm2);

            bool open = false;
            foreach (Form OpenForm in Application.OpenForms)
            {
                if (OpenForm.GetType() == typeof(Form2))
                {
                    OpenForm.Activate();
                    open = true;
                }
            }
            if (open == false)
            {
                frm2.MdiParent = this;
                frm2.Show();
                frm2.AutoScroll = true;
            }



        }

        private void viewDataToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form3 frm3 = new Form3();
          //  avoid_multiple_forms(frm3);
            bool open = false;
            foreach (Form OpenForm in Application.OpenForms)
            {
                if (OpenForm.GetType() == typeof(Form3))
                {
                    OpenForm.Activate();
                    open = true;
                }
            }
            if (open == false)
            {
                frm3.MdiParent = this;
                frm3.Show();
                frm3.AutoScroll = true;
            }
           




        }

        private void Form4_Load(object sender, EventArgs e)
        {

        }
    }
}
Posted
Updated 16-Aug-21 6:01am
v4

If you are trying to avoid opening new instances of a form but bring the existing instance to the front instead, then it's simple: add a static variable to your Form code which holds an instance of that form. Preset it to null. Handle the FormClosing event, and set it to null then as well.
In the Form Load event, check the variable. If it is null, set it to the current instance, and continue.
If it isn't, then bring that instance to the front, and Close the form you are loading.

That way, the form takes care of itself: when you try to open it, it checks if it's already there and handles it, your external code doesn't need to get involved at all.
 
Share this answer
 
If your goal is to:

1) let the run-time user create a single instance of any one type of form

2) not let the run-time user create another instance of the same type

Keep a reference to the Form instances the user, or you, create: use a List or whatev3er other data structure you want. If the user closes an instance, update your List.

If you want to use Application.OpenForms to monitor which Forms are active, you'll need to cast it to IEnumerable, like this:
// requires: using System.Linq;
bool hasFormType = Application.OpenForms.Cast<Form>().Any((Form frm) => frm is Form2);
But, it's much better not to give the user the option of creating another instance of the same type.

It's called "defensive programming," and, the idea is to stop mistakes happening, rather than deal with handling choices that should not be available
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900