Click here to Skip to main content
15,892,809 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello Experts,
I am facing opening of duplication forms.
here is my code.
C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Reflection;

namespace ServiceManagementSystem
{
    public partial class MainCont : Form
    {
        private System.Collections.ArrayList arrMainMenu = new System.Collections.ArrayList();
        private System.Collections.ArrayList arrSubMenu = new System.Collections.ArrayList();
        public System.Windows.Forms.MenuStrip AppMenu;
        public MainCont()
        {
            InitializeComponent();
        }

        private void MainCont_Load(object sender, EventArgs e)
        {
            try
            {
                this.AppMenu = new System.Windows.Forms.MenuStrip();
                // 
                // AppMenu
                // 
                this.AppMenu.BackColor = System.Drawing.SystemColors.GradientActiveCaption;
                this.AppMenu.Font = new System.Drawing.Font("Segoe UI", 11F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                this.AppMenu.Location = new System.Drawing.Point(0, 0);
                this.AppMenu.Name = "AppMenu";
                this.AppMenu.Size = new System.Drawing.Size(917, 24);
                this.AppMenu.Text = "AppMenu";
                this.Controls.Add(this.AppMenu);
                LoadMenu();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        //Menu loading
        private void LoadMenu()
        {
            arrMainMenu.Add("File");
            arrMainMenu.Add("QCS");
            arrMainMenu.Add("Help");
            arrSubMenu.Add("PSFCalling");
            ToolStripMenuItem tsm = null;
            for (int i = 0; i < arrMainMenu.Count; i++)
            {
                tsm = new ToolStripMenuItem(arrMainMenu[i].ToString());
                int j = 0;
                if (i == 0)
                    j = 0;
                else
                    j = 2;
                for (; j < arrSubMenu.Count; j++)
                {
                    if ((i == 1) && (j == 2))
                        break;
                    tsm.DropDown.Items.Add(arrSubMenu[j].ToString());
                }
                tsm.DropDown.ItemClicked += new ToolStripItemClickedEventHandler(DropDown_ItemClicked);
                this.AppMenu.Items.Add(tsm);
            }
        }

        void DropDown_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            try
            {
                CallWindow(e.ClickedItem.Text);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        //
        private void CallWindow(string myForm )
        {
            Type type = Type.GetType("ServiceManagementSystem." + myForm);
            Form c = Activator.CreateInstance(type) as Form;
            c.Show();
        }
    }
}

In this Menu Items and sub menu Items are temporally added in to arryList. and all menu generation code is temp. later I will call from database.. I want make admin authority to select menu Items particular User.
I took care for Forms name and Submenu Items text will same.
here I am not understanding how to prevent duplicate forms opening.even though form is in solution I can't use its instance name.waiting for some good solution.

Thanks
Posted
Comments
[no name] 15-Aug-12 10:03am    
Do you mean you want to c.ShowDialog() instead?
Basics Learner 15-Aug-12 10:34am    
No! it will prevent to opening another window.
need c.Show() but need to prevent duplicate windows.

To prevent opening of a duplicate, you must first know what is open. I'm not sure about the reflective lookup but let's leave that for now, and register windows when you open them:

private void CallWindow(string myForm )
{
    Type type = Type.GetType("ServiceManagementSystem." + myForm);
    Form c;
    if(!openForms.TryGetValue(type, out c) || c.IsDisposed){
      openForms[type] = c = Activator.CreateInstance(type) as Form;
    }
    c.Show();
}

private Dictionary<Type, Form> openForms = new Dictionary<Type, Form>();
 
Share this answer
 
v2
Comments
Basics Learner 15-Aug-12 10:17am    
Hi Thanks for quick response.
seems it works. I am getting errors
Error 1 The best overloaded method match for 'System.Collections.Generic.Dictionary<system.type,system.windows.forms.form>.TryGetValue(System.Type, out System.Windows.Forms.Form)' has some invalid arguments
Error 2 Argument '2' must be passed with the 'out' keyword
Error 3 The event 'System.ComponentModel.Component.Disposed' can only appear on the left hand side of += or -=

seems syntax errors
BobJanova 15-Aug-12 10:42am    
That's what I get for typing it straight in here ... though you should be able to work it out as well. Try

if(!openForms.TryGetValue(type, out c) || c.IsDisposed)
Basics Learner 15-Aug-12 10:48am    
Yes I done same thing.
I was some late here to post. Out of desk :)

Thanks for your help
BobJanova 15-Aug-12 11:07am    
Welcome. I edited the solution with these modifications.
I agree with BobJ - you need to know what's already open.

Not sure if something like this could work for you....

C#
private bool IsFormOpen(Form frm)
{
    bool retval = false;
    FormCollection fc = Application.OpenForms;
    foreach (Form Appforms in fc)
    {
       if (Appforms == frm)
       {
          retval = true;
       }
    }
    return (retval);
}


Pass the form you think you might want to open into this function....if its already open then you can decide not to open it again.

Aero
 
Share this answer
 
Comments
Basics Learner 15-Aug-12 11:54am    
As per logic it should work.I already tried this way. but it always returning false.
Basics Learner 15-Aug-12 11:58am    
if I use this way with static form name it is working. in previous application I worked in same way.

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