C#实现多个子窗体切换

1.设置MDI主菜单的MDIWindowListItem=显示子窗口的菜单tsmiWindow, 这样打开的子窗体标题将自动显示在Window菜单下;

2.Window菜单设置两个菜单,一个“下一个窗口”,一个“上一个窗口”,再加一个分隔线, 所以下面的代码从3开始比较。

 

        private void TsmiWindowNext_Click(object sender, EventArgs e)
        {
            int iItem = tsmiWindow.DropDownItems.Count;
            if (iItem>3)
            {
                int j = 0;
                for(int i=3; i                 {
                    ToolStripMenuItem stmi = (ToolStripMenuItem)tsmiWindow.DropDownItems[i];
                    
                    if (stmi.Checked)
                    {
                        j=i+1;
                        stmi.Checked =false;
                        if (j>=iItem)
                        {
                            ((ToolStripMenuItem)tsmiWindow.DropDownItems[3]).Checked=true;
                            getFormbyTitle(tsmiWindow.DropDownItems[3].Text);
                            return;
                        }
                        else
                        {
                            ((ToolStripMenuItem)tsmiWindow.DropDownItems[j]).Checked=true;
                            getFormbyTitle(tsmiWindow.DropDownItems[j].Text);
                            return;
                        }
                    }
                }
            }
        }
        
        
        private void TsmiWindowPre_Click(object sender, EventArgs e)
        {
            int iItem = tsmiWindow.DropDownItems.Count;
            if (iItem>3)
            {
                int j = 0;
                for(int i=3; i                 {
                    ToolStripMenuItem stmi = (ToolStripMenuItem)tsmiWindow.DropDownItems[i];
                    
                    if (stmi.Checked)
                    {
                        j=i-1;
                        stmi.Checked =false;
                        if (j<3)
                        {            
                            ((ToolStripMenuItem)tsmiWindow.DropDownItems[iItem-1]).Checked=true;
                            getFormbyTitle(tsmiWindow.DropDownItems[iItem-1].Text);
                            return;
                        }
                        else
                        {
                            ((ToolStripMenuItem)tsmiWindow.DropDownItems[j]).Checked=true;
                            getFormbyTitle(tsmiWindow.DropDownItems[j].Text);
                            return;
                        }
                    }
                }
            }
        }
        private void getFormbyTitle(String strTitle)
        {
            foreach(Form f in this.MdiChildren)
            {
                if (f.Text == strTitle.Substring(strTitle.IndexOf(" ")+1) && f != null)
                {
                    Type formType = f.GetType();
                    if (formType.IsInstanceOfType(f))
                    {
                        if(f.WindowState == FormWindowState.Minimized)
                        {
                            f.WindowState = FormWindowState.Normal ;
                        }
                        f.BringToFront();
                        return;
                    }
                }
            }
        }

你可能感兴趣的:(C#)