Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
3.50/5 (2 votes)
See more:
Hello Friends,

I have a MDI Parent Form (Main Form) in my c# aplication.
Also I have a child windows (Child Form 1) in my aplication.

When I do click in a option menu in my Parent Form, then the child window is appear.
when I do click again in this option menu, the secondary window is duplicated and now
I have two child windows that is appear.

But I do not want that, I want that only one child window will showing, no matter how many click i do.


Please would you give me any solutions???

Thanks in advance...
Posted

I guess you are initializing the Child Window inside the Main Form's menu click event.

Declare a Child Form object outside the function and check for null.

Ex:

C#
ChildForm childForm = null;

private void OnMenuClick (object sender, EventArgs e)
{
    if (childForm == null)
    {
        childForm = new ChildForm();
    }
    childForm.Show(); 
}


You must call Dispose when you close the ChildForm.

Mark it as answer if it is helpful
 
Share this answer
 
v3
Comments
Albin Abel 18-Mar-11 13:35pm    
Good. My 5
Venkatesh Mookkan 18-Mar-11 13:47pm    
Thank you
leocode7 18-Mar-11 13:44pm    
thanks friend .. but when i close the child windows, i can not open again ,
how i can solve this problem???

Thanks in advance.
Venkatesh Mookkan 18-Mar-11 13:46pm    
That is because you need to call Dispose() of the Child Window when window is closed.
leocode7 18-Mar-11 13:56pm    
I have writing this methods:

private void subForm1_FormClosed(object sender, FormClosedEventArgs e)
{
this.Dispose(true);
}
private void subForm1_FormClosing(object sender, FormClosingEventArgs e)
{
this.Dispose(true);
}

both methods are of chid window this child window.

in the in the parent window i wrote:
if (sub_w1 == null)
{
sub_w1 = new subForm1();
sub_w1.MdiParent = this;
sub_w1.Show();
}
this is that you tell me...

What i make now ????

thanks...
Best solution is: never use MDI! This UI style is absolutely discouraged.
See http://en.wikipedia.org/wiki/Multiple_document_interface#Disadvantages[^].

It is cumbersome in implementation and extremely inconvenient for the users. You could scare them off!
Use tab interface instead; there are other options. Actually, all "advantages" if MDI are also covered by a simple tabbing UI.

For tabbed interface samples, use this: Tabbed MDI Child Forms[^].

For more samples, search http://www.codeproject.com/info/search.aspx?artkw=tabbed[^] with tags: C#, WinForms.

—SA
 
Share this answer
 
v2
Comments
leocode7 21-Mar-11 17:33pm    
I respect your opinion about that, but the software is for an internal aplication, non comercial...
any way I use MDI because I want a Main Window and secundaries that appear in function of the main Menubar that is all.

PD: You must to see how to show the apication and you should understand me, please see this link:
http://img69.imageshack.us/i/window1programagestion.jpg/
Thanks.
Sergey Alexandrovich Kryukov 21-Mar-11 23:36pm    
Sorry, I don't understand the drive. To me, it's pretty ugly. As to "non commercial"... This is if your colleagues are worse then clients, so can be tortured by inconvenient UI... well, I understand your work could be acceptable, but the argument does not look right to me...
--SA
leocode7 22-Mar-11 10:25am    
thanks for your advice, but I can make a "Good" MDI aplication.
you will can see..

regards,
Sergey Alexandrovich Kryukov 22-Mar-11 14:05pm    
It's not about your skills, this is about a concept. If you consider MDI as good, it could be simply an indication of that you're not familiar with real good, how about that.
No matter, my point was to warn you, the conclusion is up to you... :-)
--SA
leocode7 22-Mar-11 14:58pm    
Ok. I agreement with you.
I hope I will finished this aplication fast, and in the next oportunity I will to use "tab interface instead".
But please give me any link with images and code to apreciate this modern technique.
Thanks in advance.
Your describing a non modal dialog it sounds like. What I do for this is declare and create the dialog as a member of the main form at startup/creation. When the menu item is selected the dialog is activated using Show and Hide functionality. The dialogs Closing event is listened to and just Hide the form and cancel the closing in the event handler

C#
public void OnDialogFormClosing(object sender, CancelEventArgs e)
        {
            m_DialogForm.Hide();
            e.Cancel = true;
        }


This example has the handling in the main form but you should be able to handle it in the dialog as well.
 
Share this answer
 
Comments
leocode7 29-Mar-11 13:42pm    
hello friend...
please give me more details about how I can do:
1) Create a dialog as a member of my Main window????? (I need the code example)
Another points are clear.
Thanks,

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