如果第一个mdi子表格打开,请关闭第二个mdi子表单。 [英] Close second mdi child form if first mdi child form is open.

查看:91
本文介绍了如果第一个mdi子表格打开,请关闭第二个mdi子表单。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个Windows应用程序,其中有一个包含许多子窗体的父窗体。现在我想要做的是,如果打开第一个表单,请关闭第二个表单。我有menustrip控件,一旦打开任何表单就会被禁用,如果表单关闭则启用。现在我在菜单条中添加了快捷键以打开表单。通过使用快捷方式我打开第一个表单,然后再次使用快捷方式,打开第二个表单,关闭第一个表单而不是第二个表单。



我有在c#中开发了一个windows应用程序,其中有1个父表单11个子表单。当我单击父窗体上的菜单时,将打开指定的子窗体并禁用父窗体上的menustrip。当子窗体关闭时,再次启用menustrip。这样工作正常。



但我所做的更多是为菜单创建了快捷方式,例如Ctrl + P用于打印,Alt + R用于报告,Ctrl + B用于Binder现在当打开子窗体时,如上所述禁用了menustrip,但是与菜单相关的快捷方式如Ctrl + P用于打印,Alt + R用于报告,Ctrl + B用于活页夹等都可以正常工作。我想这样,例如: - 如果我打开打印表单(Ctrl + P),那么其他快捷方式不应该工作,即Alt + R,Ctrl + B等。



我该怎么办?



我写的代码: -





在此先感谢!!!



我尝试过:



  void  MDIParent1_MdiChildActivate( object  sender,EventArgs e )
{
if this .MdiChildren.Count()> 1
{
foreach (表格childForm this .MdiChildren)
{
if (childForm!= this .ActiveMdiChild)
{
childForm.Close();
return ;
}
}
}
}

解决方案

你面临的常见问题是循环中修改的元素集:您正在遍历集合 MdiChildren ,但是一旦关闭表单,此集就会被修改。修复它的一种方法是将集合克隆到一些列表中( System.Collections.Generic.List< Form> ,在这种情况下)并使用此列表赢得' t每更改关闭;另一种方法是反向迭代元素。



但我想提出更好的建议。以下是这个想法:谁需要MDI?为什么要折磨自己并吓跑你的用户?

帮自己一个大忙:根本不要使用MDI。没有它,您可以更轻松地实现设计,质量更好。 MDI甚至被微软高度劝阻,事实上,微软将其从WPF中删除并且很难支持它。更重要的是,如果您使用MDI,您将吓跑所有用户。只是不要。请参阅:

http://en.wikipedia.org/wiki/Multiple_document_interface#Disadvantages

如何在WPF中创建MDI父窗口?



我可以解释做什么。请看我过去的答案:

如何在WPF中创建MDI父窗口?

关于在WPF中使用MDI窗口的问题

MDIContainer给出错误

如何设置最大化的子表单,最小化最后一个子表单



-SA


我已经解决了这个问题。



我在班级声明了一个整数变量 -

  public   static   int  Form_Count ; 



在菜单项目上单击ev我将以上书面方法修改为: -

首先,它将检查Form_Count是否为0,然后继续并完成工作并将Form_Count更改为1.如果Form_Count为1,则它将显示已定义如果(Form_Count < 1
{
foreach (表单形式 in Application.OpenForms)
{
if (form.GetType()== typeof (form1))
{
form.Activate();
return ;
}
}
form1 cr = new form1();
cr.MdiParent = ;
cr.Show();
menuStrip1.Enabled = false ;
Form_Count = 1 ;
}
else
{
NoMessageBox.ShowBox( 可以一次打开一个表单。 );
// NoMessageBox.ShowBox是自定义消息Box,我已经设计和开发。可以使用默认/系统消息框。
}



并在表单上关闭事件,即我已按下按钮点击,

菜单条已启用,Form_Count将再次更改为0。这样: -

 frmMain_Page.menustrip.Enabled =  true ; 
frmMain_Page.Form_Count = 0 ;
// frmMain_Page是Mdi父表。


I have created windows application in which there is a parent form with many child forms. Now what I want to do is, Close the second form if first form is opened. I have menustrip control, that is disabled once any form is opened and enabled if form is closed. Now I have added shortcut keys in menu strip for opening the forms. By using the shortcut I open the first form, then again by using shortcut, the second form is opened and the first form is closed instead of second form.

I have developed a windows application in c#, where there is 1 parent form 11 child forms. When I click on menu on parent form, the specified child form is opened and the menustrip on parent form is disabled. when the child form is closed, menustrip is enabled again. This is working fine.

But further what I did is, created shortcuts for menus like Ctrl + P for printing, Alt + R for Report, Ctrl + B for Binder, etc. Now when the child form is opened, the menustrip is disabled as mentioned above, but the shortcuts associated with the menus like Ctrl + P for printing, Alt + R for Report, Ctrl + B for Binder, etc are all working. I want that, for eg:- if I open Print Form (Ctrl + P), then other shortcuts should not work i.e. Alt + R, Ctrl + B, etc.

How can I do that ???

The code I wrote :-


Thanks in Advance !!!

What I have tried:

void MDIParent1_MdiChildActivate(object sender, EventArgs e)
        {          
            if (this.MdiChildren.Count() > 1)
            {
                foreach (Form childForm in this.MdiChildren)
                {
                    if (childForm != this.ActiveMdiChild)
                    {
                        childForm.Close(); 
                        return;
                    }
                }
            }
        }

解决方案

You face the usual problem with the set of elements modified in a loop: you are traversing the set MdiChildren, but as soon as you close a form, this set get modified. One approach to fix it is to clone the set into some list (System.Collections.Generic.List<Form>, in this case) and operate with this list which won't change on every Close; another approach is to iterate elements in reverse.

But I wanted to suggest something better. Here is the idea: who needs MDI, ever? Why torturing yourself and scaring off your users?
Do yourself a great favor: do not use MDI at all. You can do much easier to implement design without it, with much better quality. MDI is highly discouraged even by Microsoft, in fact, Microsoft dropped it out of WPF and will hardly support it. More importantly, you will scare off all your users if you use MDI. Just don't. Please see:
http://en.wikipedia.org/wiki/Multiple_document_interface#Disadvantages,
How to Create MDI Parent Window in WPF?.

I can explain what to do instead. Please see my past answers:
How to Create MDI Parent Window in WPF?,
Question on using MDI windows in WPF,
MDIContainer giving error,
How to set child forms maximized, last childform minimized.

—SA


I have solved the problem.

I declared a integer variable at class level-

public static int Form_Count;


And on menu Item click event I modified the above written method as:-
First, it will check if Form_Count is 0, then proceed and do the work and change Form_Count to 1. If Form_Count is 1 then it will show the defined message.

if (Form_Count < 1)
            {
                foreach (Form form in Application.OpenForms)
                {
                    if (form.GetType() == typeof(form1))
                    {
                        form.Activate();
                        return;
                    }
                }
                form1 cr = new form1();
                cr.MdiParent = this;
                cr.Show();
                menuStrip1.Enabled = false;
                Form_Count = 1;
            }
            else
            {
                NoMessageBox.ShowBox("Can open one form at a time.", "Form");
                //NoMessageBox.ShowBox is custom message Box, I have designed and developed. can use the default/system message box.
            }


And On the form Close event i.e. I have written on button click,
Menu strip is enabled and Form_Count will be changed to 0 again. In this way:-

frmMain_Page.menustrip.Enabled = true;
frmMain_Page.Form_Count = 0;
//frmMain_Page is the Mdi Parent Form.


这篇关于如果第一个mdi子表格打开,请关闭第二个mdi子表单。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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