Click here to Skip to main content
15,567,475 members
Please Sign up or sign in to vote.
2.00/5 (2 votes)
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:

C#
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.

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
 
Share this answer
 
Comments
BillWoodruff 9-Feb-16 0:37am    
+4 Countering unwarranted down-vote.
I have solved the problem.

I declared a integer variable at class level-
C#
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.
C#
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:-
C#
frmMain_Page.menustrip.Enabled = true;
frmMain_Page.Form_Count = 0;
//frmMain_Page is the Mdi Parent Form.
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900