在MDI形式prevent相同的子窗口多次 [英] Prevent same child window multiple times in MDI form

查看:125
本文介绍了在MDI形式prevent相同的子窗口多次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的C#桌面应用程序,在录入的形式相同的子窗口,当你点击菜单使用打开,而该窗口的第一个实例是present。 我怎样才能prevent的子窗口中的MDI窗体这些多个实例?

I am working on c# desktop application, In MDI form the same child window getting opened when you click on menu, while first instance of that window is present. How can I prevent these multiple instances of child windows in MDI form?

推荐答案

您可以检查的形式已经被打开了:

You can check if the form has been opened already:

  Form instance = null;

  // Looking for MyForm among all opened forms 
  foreach (Form form in Application.OpenForms) 
    if (form is MyForm) {
      instance = form;

      break; 
    }

  if (Object.ReferenceEquals(null, instance)) {
    // No opened form, lets create it and show up:
    instance = new MyForm();
    instance.Show();
    ...
  }
  else {
    // MyForm has been already opened

    // Lets bring it to front, focus, restore it sizes (if minimized)
    if (instance.WindowState == FormWindowState.Minimized)
      instance.WindowState = FormWindowState.Normal; 

    instance.BringToFront();

    if (instance.CanFocus) 
      instance.Focus();
    ...
  }

这篇关于在MDI形式prevent相同的子窗口多次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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