Click here to Skip to main content
14,388,839 members
Rate this:
Please Sign up or sign in to vote.
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;
                    }
                }
            }
        }
Posted
Updated 8-Feb-16 9:00am
v5
Comments
Gautham Prabhu K 8-Feb-16 5:59am
   
Is it not working? or what is the error or exception your getting?
Ank_ush 8-Feb-16 8:00am
   
it works but the problem is that, I want to close second form not the first form. I have written the code that will disable menustrip control on parent form, once any child form is opened. But for every menu I have assigned shortcut keys like Ctrl + P, Alt + L, etc. Once any child form is opened the menustrip is disabled, but the shortcut keys associated with them are working. Either the shortcut keys should be disabled or else the opening of should be prevented(user may open more than 2 forms at a time by using shortcut keys).
Sinisa Hajnal 8-Feb-16 8:33am
   
You could go through parents MDIChildren collection and close those you want closed at the time you disable the menu. Also, shortcuts should call menu.PerformClick. If it is disabled it simply won't work :) Or you could before opening the form on key handler check if the form should open and prevent it.
Ank_ush 8-Feb-16 12:12pm
   
how can I do it?
BillWoodruff 8-Feb-16 6:25am
   
You seem to be saying that you want one of two Forms open at any one time, but then you describe using a menu shortcut that opens the second form and closes the first one ... but, that is exactly what you want to happen, I think.
Ank_ush 8-Feb-16 8:02am
   
I want the first opened form to be remained opened, not the second form.
BillWoodruff 8-Feb-16 8:20am
   
I have worked with MDI in the past, and understand the tricky issues dealing with MdiChildForms that often drive programmers nuts. I'm willing to help you, but you need to clearly state what you want your app to do.

1. describe each type of child window

1.a. does any child window(s) always stay open ? if so, which ones ?

2. describe the menu structure: top-level menu items, and sub-menu items

2.a. do you disable menu items ? disable sub-menu items ?

2.b. your MdiChildForms: they show the close button, but you block them from being closed ? If so, how are you handling closing the Application (this is one of the tricky parts).

3. describe precisely what the logic is that you are using to decide which child forms are open, or closed.
Ank_ush 8-Feb-16 9:58am
   
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 ???
BillWoodruff 8-Feb-16 13:34pm
   
Note: What is the point of having a Menu Item where you can select one of many MdiChildForms to open, and then hiding the menu when one is open ? Why not have that Menu open all the time ... if you need to hide other MenuItems ... hide those.

Now, we're getting somewhere. But, you need to go and rewrite your original post here to indicate that the problem is not as you describe it, there. You imply that closing one child form automatically opens another. Now you tell us that the condition for the menu items being active is that all child forms are closed.

You have also run into a well-known bug ... ShortCuts still work when their container is disable ... in the MenuStrip Control that Microsoft has been told about many times and, so far, has said they will not fix.

I will respond further after you revise your original post here so that people will not waste time because your question is not clear.
Ank_ush 8-Feb-16 14:02pm
   
You got what I want, Please help me. !!!
Thanks in Advance.......
BillWoodruff 9-Feb-16 0:38am
   
No, I do not have what you "want." I'll respond when you make your original post clearer.
Ank_ush 9-Feb-16 0:46am
   
I posted the answer, works for me fine...
Muhammad Idrees GS 8-Feb-16 6:44am
   
Did your logic only depends on first and second form (means form number sequence), or do you have some specific requirements with form names/keys? I think it would be more logical if you can define an interface with a Key (int/string etc.) and let each form implement this interface with own unique Key. Then in this condition you can check for that key which you want to close or which you want to keep opened.
Rate this:
Please Sign up or sign in to vote.

Solution 1

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
   
Comments
BillWoodruff 9-Feb-16 0:37am
   
+4 Countering unwarranted down-vote.
Rate this:
Please Sign up or sign in to vote.

Solution 2

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.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100