Click here to Skip to main content
15,889,216 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hi all,

I am working with MDI form with 25 Child forms,

Requirement: form name 1,2,3,4,5,6,7,8,9......

1. Form 1 has open.
2. If user clicked on form 2 to open.
3. We need to ask user to close form 1 are not ? If ok (form 1 will closed and form 2 will open)
Posted
Updated 7-Sep-11 21:32pm
v2
Comments
Uday P.Singh 8-Sep-11 3:30am    
does your form1 is your main form?
manoharnch 8-Sep-11 3:32am    
yes, all 1 to 25 forms are in main MDIFORM. form1 also in MDIFORM

So what is the problem?
Like how you r opening child form, likewise you will find inbuild method to close form.
 
Share this answer
 
Comments
manoharnch 8-Sep-11 3:37am    
close(); method is not working sir.
i need to show user to form1 opened do u want to close the form1 YES NO
if YES form1 should close and form2 should open.
Have a look on that

Introduction to MDI Forms with C#[^]
 
Share this answer
 
Comments
Md. Rashim Uddin 8-Sep-11 3:50am    
Please go through the link and download the Demo Project....Then you might solve your problem...The demo project is working perfectly
hi manoharnch,

you can get the active child window opened in your MDI form using
C#
this.ActiveMdiChild
 
Share this answer
 
The easiest way is to keep a form level variable which refers to your open instance. When the user requests Form2, check, and if it is not null, then ask and close if necessary.

C#
Form1 openForm1 = null;
...
openForm1 = new Form1();
openForm1.Show();
...
if (openForm1 != null)
   {
   if (MessageBox.Show("Close it?") == DialogResult.OK)
      {
      openForm1.Close();
      Form2 f = new Form2();
      ...
      }
   }
You will also want to handle the Form1.FormClosed event to set the openForm1 variable to null again.
 
Share this answer
 
Comments
[no name] 8-Sep-11 3:48am    
there is no need to keep a form level variable
OriginalGriff 8-Sep-11 3:57am    
It is true you can use the MDIChildren list, but then you have to search it for an instance of the Form1 class, when it may contain other forms. Keeping a single class level instance ensures that only a single Form1 instance is open at any one time.
Your solution relies on there either being only one MDI child at a time (in which case why use MDI at all?) or that the user does not activate a different MDI child before trying to open Form2

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