Click here to Skip to main content
14,391,100 members
Rate this:
Please Sign up or sign in to vote.
See more:
hello,

i have a MDI parent form and inside that i have menu bar for opening child forms.
when i open a child form it opens inside MDI parent form.
and when i open second child form the previous one is not closing.

the opened child form should be closed when i open new child form.....

how can i achieve this...

please help......
Posted
Comments
StM0n 11-Aug-11 6:31am
   
so... only one (specific?) child is allowed to be shown? Do I get that right...?
yogesh89 16-Aug-11 6:15am
   
yes you are right.....
StM0n 16-Aug-11 6:51am
   
Ok... my guess would be, that you have (hopefully) plenty solutions to choose from :)
nagendrathecoder 11-Aug-11 7:14am
   
Please mention do you want only one instance of a particulat child form open or you want only and only one instance of any of the child form open at a time.
Rate this:
Please Sign up or sign in to vote.

Solution 2

You have to write code for that.

Check whether MDI has child form or not using this.HasChildren and close that instance of form using childForm.Close().
   
Comments
Pravin Patil, Mumbai 11-Aug-11 6:41am
   
Very nice answer....
my 5
RaviRanjanKr 11-Aug-11 9:04am
   
Nice Answer, My 5+
Rate this:
Please Sign up or sign in to vote.

Solution 1

When opening a form use from1.ShowDialog() instead of form1.Show(). So, this will not allow you to open other without closing the current from.
   
Comments
Pravin Patil, Mumbai 11-Aug-11 6:42am
   
But it will also prevent clicking on the menu bar or toolbar as long as current form is open.....
Toniyo Jackson 11-Aug-11 6:44am
   
Yup. This is what OP needs
Rate this:
Please Sign up or sign in to vote.

Solution 3

i guess you wanted that only one instance of a particular child form should be open at a time.
For this, you need to check if the instance of that child form is null then only open new instance OR otherwise if instance is already created just bring to front that instance.
   
Rate this:
Please Sign up or sign in to vote.

Solution 4

private WhatEverClassThisMayBe _TheOneAndOnlyChild;

public|private void showChild() {

    if (this._TheOneAndOnlyChild != null) {
        this._TheOneAndOnlyChild.Close();
    }

    this._TheOneAndOnlyChild = new WhatEverClassThisMayBe();

    this._TheOneAndOnlyChild.Show();

}


Something like that, would be my guess...
   
Comments
nagendrathecoder 11-Aug-11 9:00am
   
If OP really wants this.....i can't imagine why he needs something like this.
Anyhow it is good answer
StM0n 11-Aug-11 9:04am
   
Thanks for the reply... and honestly, I either didn't figure it out, what the OP's trying to achieve... ;)

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