防止打开重复的Winform. [英] Preventing opening duplicate winforms.

查看:91
本文介绍了防止打开重复的Winform.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好专家,
我正面临着重复表格的打开.
这是我的代码.

Hello Experts,
I am facing opening of duplication forms.
here is my code.

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


在此菜单项和子菜单项中,临时将项添加到arryList中.并且所有菜单生成代码都是临时的.稍后我将从数据库中调用.我想让管理员有权选择菜单项特定用户".
我照顾了表单名称和子菜单项"文本,它们相同.
在这里,我不了解如何防止打开重复的表单.即使表单处于解决方案中,我也无法使用其实例名称.等待一些好的解决方案.

谢谢


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

推荐答案

要防止打开重复项,必须首先知道打开了什么.我不确定反射式查找,但现在暂时将其保留,并在打开它们时注册窗口:

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


我同意BobJ-您需要知道已经打开的内容.

不知道这样的事情是否可以为您服务....

I agree with BobJ - you need to know what''s already open.

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

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


这篇关于防止打开重复的Winform.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆