使用MDI窗体结构,主窗体上的各个菜单项对应各个子窗体。比如点击某个菜单项打开某个子窗体的代码如下:
private void 车辆称重ToolStripMenuItem_Click_1(object sender, EventArgs e)
        {
            Weighing.frmWeighing weighing = null;
            foreach (Form f in this.MdiChildren)
            {
                if (f is Weighing.frmWeighing)
                {
                    weighing = (Weighing.frmWeighing)f;
                    break;
                }
            }
            if (weighing != null)
            {
                weighing.Show();
                weighing.Focus();
            }
            else
            {
                weighing = new Weighing.frmWeighing();
                weighing.MdiParent = this;
                weighing.Show();
                weighing.Update();
            }
        }而其他的菜单项点击代码大同小异,实际上只是改动上面代码中的weighing和Weighing.frmWeighing。我想写个小函数,如:
private void openMDI(object xxx, object yyy)
        {
......
}
这样只要把各个子窗体的对应的xxx、yyy作为参数传进来调用一下函数就可以打开相应的子窗体,但这个小函数我写不出来,请高手帮帮忙吧,谢谢了!

解决方案 »

  1.   

    你要的是单实例窗体。
    使用  Singleton<Form1>.Instance.Show();
    /// <summary>
       /// generic for singletons
       /// </summary>
       /// <typeparam name="T"></typeparam>
       public class Singleton<T> where T : new()
       {
          // ctor
          protected Singleton()
          {
             if(Instance != null)
             {
                throw (new Exception("You have tried to create a new singleton class where you should have instanced it. Replace your \"new class()\" with \"class.Instance\""));
             }
          }      public static T Instance
          {
             get
             {
                return SingletonCreator.instance;
             }
          }      class SingletonCreator
          {
             static SingletonCreator()
             {         }
             internal static readonly T instance = new T();
          }
       }
      

  2.   

    说说思路
    1,首先,通过菜单本身,应该能知道需要控制的窗体的类型,ToolStripMenuItem_Click(object sender, EventArgs e)应该能获取窗体的类型,例如可以使用MenuItem.Tag来记录,这样string typename = (string)sender.Tag;
    2,查找现有的窗体,一般不使用类型来匹配,而是根据用途设置一个Key,这个Key同样可以记录到窗体的Tag属性里。
    3,查找就直接判断Form.Tag = "Key"
    4,找到就容易了,你的代码已经处理了
    5,找不到,则需要创建,因为类型已经知道,那就直接使用类型创建一个Form,然后接着处理,你已经处理了。
      

  3.   

    我的想法和3楼差不多,结合你现有的代码,可以合并所有子菜单的事件为一个。
    首先要在主窗体的Load或构造中设置每个子菜单项的Tag为对应的子窗体类型。
    如:toolStripMenuItem1.Tag=typeof(frmWeighing); 
       toolStipMenuItem2.Tag=typeof(frmWeighing2); private void 车辆称重ToolStripMenuItem_Click_1(object sender, EventArgs e)
            {
               ToolStripMenuItem tsm = sender as ToolStripMenuItem;
               Type type=(Type)tsm.Tag;//窗体类型
                Form subForm = null; //点击菜单后要打开的窗体
                foreach (Form f in this.MdiChildren)
                {
                    if (f is type)
                    {
                        subForm = f;
                        break;
                    }
                }
                if (subForm != null)
                {
                    subForm.Show();
                    subForm.Focus();
                }
                else
                {
                    subForm = (Form)Activator.CreateInstance(type);
                    subForm.MdiParent = this;
                    subForm.Show();
                    subForm.Update();
                }
            }
      

  4.   

    上面有个语法错误, 
     
                    if (f.GetType==type)
                    {
                        subForm = f;
                        break;
                    }