Click here to Skip to main content
14,383,744 members
Rate this:
Please Sign up or sign in to 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 22: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
Rate this:
Please Sign up or sign in to vote.

Solution 1

So what is the problem?
Like how you r opening child form, likewise you will find inbuild method to close form.
   
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.
Rate this:
Please Sign up or sign in to vote.

Solution 3

Have a look on that

Introduction to MDI Forms with C#[^]
   
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
Rate this:
Please Sign up or sign in to vote.

Solution 4

hi manoharnch,

you can get the active child window opened in your MDI form using
this.ActiveMdiChild
   
Rate this:
Please Sign up or sign in to vote.

Solution 2

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.

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.
   
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, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100