zoukankan      html  css  js  c++  java
  • 如何在C#的MDI窗体中同样的子窗体只显示一次

    /// <summary>
    /// 打开窗体
    /// </summary>
    /// <param name="mdiChildTypeString"></param>
    /// <returns></returns>     private void ShowWindow(string mdiChildTypeString)
    {

         Form currentMdiChild
    = null;

        
    if (!IsMDIChildFormExist(mdiChildTypeString))
         {
             Assembly assembly
    = Assembly.GetExecutingAssembly();
             Type typForm
    = assembly.GetType(mdiChildTypeString);

            
    //关于InvokeMember用法,不明白的可以查看:http://msdn.microsoft.com/zh-cn/library/de3dhzwy(VS.80).aspx
             Object mdiChild = typForm.InvokeMember(
                
    null,
                 BindingFlags.DeclaredOnly
    | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.CreateInstance,
                
    null,
                
    null,
                
    null);

            
    if (mdiChild != null)
             {
                 currentMdiChild
    = mdiChild as Form;
                 currentMdiChild.MdiParent
    = this;
                 currentMdiChild.WindowState
    = FormWindowState.Maximized;
                 currentMdiChild.Show();
                 currentMdiChild.Focus();
             }

         }

    }

    /// <summary>
    /// 检查MDI子窗体是否存在
    /// </summary>
    /// <param name="mdiChildTypeString"></param>
    /// <returns></returns>        
    private bool IsMDIChildFormExist(string mdiChildTypeString)
    {
         Form currentMDIChild
    = null;
        
    foreach (Form mdiChildFrm in this.MdiChildren)
         {
            
    if (mdiChildFrm.GetType().ToString() == mdiChildTypeString)
             {
                 currentMDIChild
    = mdiChildFrm;
                
    break;
             }
         }

        
    if (currentMDIChild != null)
         {
            
    if (currentMDIChild.WindowState == FormWindowState.Minimized)
             {
                 currentMDIChild.WindowState
    = FormWindowState.Normal;
             }
             currentMDIChild.TopMost
    = true;
             currentMDIChild.Activate();
             currentMDIChild.Focus();
            
    return true;
         }
        
    else
         {
            
    return false;
         }

    }

    //调用方法如下

     private void toolStripButton1_Click(object sender, EventArgs e)
            {
                ShowWindow(typeof(Form1).ToString());          
               
            }

    private void toolStripButton2_Click(object sender, EventArgs e)
            {
                ShowWindow(typeof(Form2).ToString());  
            }

  • 相关阅读:
    使用活字格搭建企业级web应用--办公用品管理系统
    怪兽级性能,用代码玩转Excel!葡萄城强势发布Spread表格组件
    无需编码,轻松搭建企业采购管理软件
    NHibernate变的简单
    Easyui + jQuery表单提交 给 Controller patr1
    Easyui datagrid 批量编辑和提交
    C# 网络编程 Part.1
    提高你开发效率的十五个Visual Studio 2010使用技巧
    System.IO
    C#路径,文件,目录,I/O常见操作
  • 原文地址:https://www.cnblogs.com/jerrychenfly/p/1879695.html
Copyright © 2011-2022 走看看