且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如果第一个mdi子表格打开,请关闭第二个mdi子表单。

更新时间:2023-02-06 19:19:44

你面临的常见问题是循环中修改的元素集:您正在遍历集合 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.