Click here to Skip to main content
15,999,596 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am developing an application on c# where i am using mdi parent form..When i am moving the child forms extreme right or extreme bottom the scroll bars appear and form go out of bound.How to control this bound of mdi parent, so that child forms don't go out of bound..please help..
Thanx
Posted

Well, you can do it but… do yourself a great favor: do not use MDI at all. You can do much easier to implement design without it, with much better quality. MDI is highly discouraged even by Microsoft, in fact, Microsoft dropped it out of WPF and will hardly support it. More importantly, you will scare off all your users if you use MDI. Just don't. I can explain what to do instead.

Where did you see any decent UI which would use MDI and be at least professional? Nowhere.

Please see:
http://en.wikipedia.org/wiki/Multiple_document_interface#Disadvantages[^] — Wikipedia,
Question on using MDI windows in WPF[^],
MDIContainer giving error[^] — SA,
How to Create MDI Parent Window in WPF?[^] — Wayne Gaylard.

Stop torturing yourself and chase off your users. Use the ideas I tried to explain in my answered referenced above, implement nice tabbed interface or something else and be happy.


—SA
 
Share this answer
 
v3
Comments
Member 11603988 21-Apr-15 12:15pm    
While your opinion is appreciated, the point of this board is to also answer the question that was asked. If you have no intention on answering the question, please also refrain from pushing your opinion.
Sergey Alexandrovich Kryukov 21-Apr-15 12:49pm    
If I refrained for "pushing my opinion", as you called it, I would not be able to help people, who are really asking the opinions. If you are not one of then, you are as free to ignore my opinion as I am free to advise what I think is right or useful.

This is me who could say "don't push me into doing what you think is right", but I'm not going to tell that to anyone, because I want to support equal freedom for everyone, excluding the cases when one can hurt others.

You also try to manipulate the readers into thinking that my post is not an answer. But this is really an answer and the practical and useful advice. I really advise the inquirer and your to follow it. What you do with this advice is your decision.

—SA
Note: I agree with esteemed colleague, and mentor, SAKruykov, that MDI based WinForm applications are not good practice now.

A few comments:

1. I think your question confuses moving vs. re-sizing of the Child Forms: you can't "move" a Form by its right or bottom; you can only resize.

a. ... under 'normal' conditions, when the Child Form starts off completely contained in the MDI Parent Form ... you can't resize an MDI Child Form outside its MDI Parent boundaries.

b. so: the real issue here must be: moving the Child Form.

c. the "dirty fact" here, a flaw, imho, in MDI architecture, is the fact that: once you have moved part of an MDI Child Form outside its MDI Parent Form, you can re-size it !

2. You also may have just "crashed against" another one of the real flaws in the MDI implementation: even if you set the 'AutoScroll property of the MDI Parent Form to 'false: moving an MDI Child Form so it extends, outside the Parent Forms' boundaries: will cause scrollbars to appear on the MDI Parent Form.

So: what to do if you must use MDI, and you want to keep the Child Forms in the MID Parent Form window boundaries when you Move them ?

There are several events of the child Form you could possibly use
ClientSizeChanged
LocationChanged
SizeChanged
RegionChanged
None of these Events expose a custom EventArgument structure that would allow you to cancel the action they reflect. None of them expose a symmetric pair of Begin/End events, with a "cancel" option in the 'Begin... Event as some other .NET control's events do.

So you are going to have, somehow, undo the effect of a Move when the Child Form goes outside the bounds of the MDI Parent Form, and that's complicated by the "dirty fact" mentioned above.

You can exploit this technique for determining whether the MDI Child Form is, or is not, fully contained in the MDI Parent Form: whenever the MDI Child Form is completely within the MDI Parent Form: then the intersection of the bounding rectangles (when derived correctly from the appropriate co-ordinate space of the two Forms) of those two Forms will be equal to the bounding rectangle of the Child Form (details to follow).

So in the MDI Parent Form ... here named 'Form1 ... :
C#
// create a new MDI Child Form
Form2 f2 = new Form2();

// think about why this must be a public static property !
public static Rectangle MdiParentBounds { get; set; }

private void Form1_Load(object sender, EventArgs e)
{
    f2.MdiParent = this;
    f2.Show();
    // think about why this must be the ClientRectangle here !
    MdiParentBounds = this.ClientRectangle;
}

private void Form1_SizeChanged(object sender, EventArgs e)
{
// think about why this must be the ClientRectangle here !
    MdiParentBounds = this.ClientRectangle;
}Now in the MDI Child Form: ... here named Form2 ...:private Rectangle childBounds;

private Rectangle interSectRect;

private void Form2_LocationChanged(object sender, EventArgs e)
{
    // think about why we use 'Bounds here, and not ClientRectangle !
    childBounds = this.Bounds;

    interSectRect = Rectangle.Intersect(Form1.MdiParentBounds, childBounds);

    if ( interSectRect == childBounds)
    {
        MessageBox.Show("child form is contained");
        // all happy now
    }
    else
    {
        MessageBox.Show("child form is not contained");
        // and now what are you going to do ?
    }
}
This should give you enough information, and code, to get started on being able to detect when an MDI Child Form extends outside the boundaries of its MDI Parent Form.

What you are going to have to do to move the Child Form which has gone outside the boundaries back so that it is within the boundaries, is left for you to go to work on.

Hint: you are going to determine which "edges" of the Child Form are outside, and what offset you need to use, vertically and horizontally, to relocate it back. And if it's been re-sized also, while outside the MDI Parent's bounds: welcome to further complications !

Of course: you could take the "cheap way out," and just change the size of the MDI Child Form to be some size you know will fit in the MDI Parent Form, and snap it back to upper-left in the MDI Parent: but, surely, you are an ambitious programmer who won't just settle for the "easy way out" :)

best, Bill
 
Share this answer
 
v4

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