Click here to Skip to main content
15,892,517 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear Friends,

I am learning C# windows application. I am working on an MDI form and facing a problem that how to keep a sub-form of a child form within the MDI form. Let me describe the problem...

1.I have an MDI form named 'First Form'.
2.The MDI form contains menubar named 'Open Second Form'.
3.Clinking on the 'Open Second Form' opens a child form named 'Second Form'.
4.the 'Second Form' remains within the MDI form as it is programmed as child of MDI.
5.Now, the 'Second Form' contains a command button 'Open Third Form'.
6.Clicking on the button opens a new form named 'Third Form'.

this third form does not remain within the MDI form when I drag it and keep outside of MDI. so, is there any solution of this problem?

or how can I freeze the third form?


Thank You,

Mayank

What I have tried:

I don't understand this problem, i have seen almost all properties for MDI and Child forms but couldn't find any solution.
Posted
Updated 2-Sep-16 20:11pm

I'd say: this is a really bad idea to use MDI. Check this: WindowsApplication with MDI[^]
 
Share this answer
 
If at all possible, I suggest you do not use the MDI WinForm architecture for your application; it's outmoded; it's visually unappealing; and, there are better ways to construct a UI that has contained elements (use UserControls, or Panels, or use a TabControl).

A WinForm can never be both an MDIParent Form and an MDIChild Form; I hope you didn't have that in mind, here. While you can, indeed, make a Form contained within another Form (not an MDIParent Form), I strongly suggest you never do that.

Suggestions:

1. in your Main Form (the MDIParent), create instances of all the Forms you will need to show as MDIChild Forms.

2. show these Child Forms when you need to.

3. if you want an event in a Child Form to cause some change in the MDI UI, like opening another Child Form:

3.a. in ChildForm1:
C#
public Action NotifyMainFormToOpenChildForm2;

private void btnNotify_Click(object sender, EventArgs e)
{
    if (NotifyMainFormToOpenChildForm2 != null)
    {
        NotifyMainFormToOpenChildForm2();
    }
}
3.b. in the MDI Parent Form:
C#
private ChildForm1 cForm1;
private ChildForm2 cForm2;

private void MDIParentForm_Load(object sender, EventArgs e)
{
    cForm1 = new ChildForm1();
    cForm1.MdiParent = this;

    cForm1.NotifyMainFormToOpenChildForm2 += NotifyMainFormToOpenForm2;

    cForm2 = new ChildForm2();
    cForm2.MdiParent = this;

    cForm1.Show();
}

private void NotifyMainFormToOpenForm2()
{
    cForm2.Show();
}
In this example we inject a pointer to the executable code that opens ChildForm2 (in the MDIParentForm) into ChildForm1. We used a delegate to do this, a delegate of Type Action which gives us an easy to use syntax.

When the Button is clicked in ChildForm1, it checks to see if there is a valid reference to code in the Action delegate, and, if there is, it executes that code which is located in the handler on the MDIParentForm, and that code show ChildForm2.

Re the problem of being able to drag MDI Child Forms so they are partially outside the boundaries of the MDI Parent Form: well, that's one of the flaws of the MDI model, imho. Getting that to work right requires using the Windows API to suppress showing ScrollBars because of an internal flaw in the Control.

I have run out of time for today (GMT +7), but if you post another question about the issue of constraining Child Forms to the boundaries of an MDI Parent Form ... and you have made some attempt to solve that problem for yourself, I will respond.
 
Share this answer
 
v2

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