C#: How to change the region containing MDI children? - c#

When creating a MDI parent, the entire "inside" of the form is made the MDI region.
If you add a menustrip to the MDI parent, the MDI region is scaled down a bit to make room for the menustrip.
But if you add a panel to the top of the MDI parent, the entire inside is still the MDI region. Which means that you can move MDI children up behind the panel, and hide their title line. If you move MDI children behind a menustrip, scrollbars appear, and you can scroll higher up to access the title line. But the scrollbars doesn't appear when you are using a panel instead of a menustrip. Because the MDI region doesn't know about the panel, I suppose.
How can I scale the MDI region to start below a given Y value?

Well the short of it is you can't modify the MDI parent/container window in .Net. The window is still there and you can find it with Win32 apis if you really desire.
However, what you describe as your goal is doable. Since there isn't any code I can't tell what your doing wrong, but the following demonstrates this working:
public class Form1 : Form
{
static void Main(string[] args) { Application.Run(new Form1()); }
public Form1()
{
this.IsMdiContainer = true;
Panel test = new Panel();
test.Dock = DockStyle.Top;
test.Height = 100;
this.Controls.Add(test);
Form child = new Form();
child.MdiParent = this;
child.Text = "Child";
child.Show();
}
}

If csharptest.net is right, and one cannot change the MDI region, I've found two ways to do it:
Add multiple MenuStrip objects (each of those moves the MDI region 24 pixels lower on the parent form).
Use the LocationChanged event of the MDI children, and manually check that their Y value is larger than the border you wish to keep them below.

Related

Specify the location of a maximized form?

I have an MDI app, and I have recently added a new control to the MDI container, which covers the client area (the area where the children appear and function). The trouble is, when they try to maximise their forms, it appears behind the sidebar:
Original form for comparison (to see the overlap)
I know that I can restrict the total size of each form that gets maximised, using the Form.MaximumSize property, however I'm not sure how to set the x/y location of the form to sit next to the sidebar.
I could use the side panel as a marker, i.e. x = sidepanel.Width because that will never change, but I don't know how to apply that to a maximised form.
Alternatively, is it possible to give the sidebar the same behaviour as the menu bar? That is - the menu bar is not considered to be in the client area; so when a form is maximised it will not overlap the child form?
Ok, so after some fiddling around, I found the main problem was that since I was adding the control dynamically, control of it was a bit difficult. So instead I did this:
Added a panel (using the forms designer) to the MDI Parent
Set the Dock property to "left"
Added the custom control dynamically to the panel:
SidePanel = new Menu_SidePanel();
SidePanel.Location = new System.Drawing.Point(0, 0);
SidePanel.Anchor = (AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left);
panel1.Controls.Add(SidePanel);
Now, because the panel has been added to, and "docked" on the Parent form, the rest of the child forms, when maximised, use the Side Panel as another boundary.

How can I set the size of the child form base from the free space of the mdi container form in C#?

My problem is that I want the child form to occupy the free space of the MDI container
Here's the code I tried
In my MDI container form load event I have this line of code calling the child form
childform = new ppt.MyChildForm();
childform.MdiParent = this;
childform.Size = childform.MdiParent.ClientSize;
childform.Show();
But what happen is that I think the child form is larger than its parent because it contains a scrollbar how can I fix this?
If AutoSize property is set to true, changing of Size has no effect.
A better practice is to set the Dock property of the child form to DockStyle.Fill. It will always fill the entire client size of its parent, so you have not to worry about parent resizing.

how to show cascade windows in Splitcontainer panel2 c#

In MdiParent toolstripmenuItem, I'm writing the code to show all the windows in cascade or Tile Horizontal style.
My code is:
this.LayoutMdi(MdiLayout.Cascade);
this.LayoutMdi(MdiLayout.TileHorizontal);
This code will work in mdi parent only. But now I'm using a Split container in my Parent Form. In Panel1 I have buttons to Show the Form. In Panel2 My Forms will display, as:
Forms.paymentPaid paidFm = new SalesandPurchases.Forms.paymentPaid();
paidFm.MdiParent = this;
paidFm.Left = (this.myPanel.Width - paidFm.Width) / 2;
paidFm.Top = (this.myPanel.Height - paidFm.Height) / 2;
myPanel.Controls.Add(paidFm);
paidFm.Show();
Now Because of my Split Container my code( this.LayoutMdi(MdiLayout.Cascade)) is not working for cascade the windows in Panel2. Please tell me any other way.
By merely changing the container of your controls without moving the MDI container itself will not work - as you have experienced. I think what you will need to do is to move your MDI container to the panel in which you want to display the child windows (panel 2). Basically you now want to have what you had before in the main window of your form in panel 2 of your split container.
I hope this helps.

Resizing a child form controls when resizing a Parent

I'm trying to figure out a easy way to re-size my child form based on the size of the parent form. i.e The child form pops out of the parent form (FixedToolWindow).
To do this presently i'm achieving this by having a OnParentResize event in my form child class. i.e
void OnParentResized(object sender, EventArgs e)
{
//Resize of the form shall be made only when the form is not minimized
if (parent.WindowState != FormWindowState.Minimized)
{
int iWidth = parent.Size.Width;
int iHeight = parent.Size.Height;
double dXFactor = (double)iWidth / (double)this.Width;
double dYFactor = (double)iHeight / (double)this.Height;
this.Scale(new SizeF((float)dXFactor, (float)dYFactor));
}
}
The line this.Scale(new SizeF((float)dXFactor, (float)dYFactor));
scales all the controls in my child form.
When i use this i presume that whenever the parent form resizes, my child form does as well. Apparently i face a problem here, all the controls inside the child form are anchored to top-left.
Initially all the controls in my child form are of normal size.
Parent form is resized to make it small, the child form shrinks to the same factor as well.
Now i increase the size of my parent form back to its original size. The size of the controls on the child form now increases by a higher ratio. And also the controls appear out of place.
Can anyone suggest a better approach for such situations.
Cheers
Change in Parent From isMdiContainer as true, childform border style as none, then
in Parent form edit code like this in vent where you want.
foreach (var mdiChild in MdiChildren)
mdiChild.Close();
var childobj= new childform {MdiParent = this, Dock = DockStyle.Fill};
childobj.Show();
Edit the Controls to by an Anchor Property.
Hope it may helps to you.
I guess you have to use the ClientRectangle's width and height instead of the Window width and height. Because your client rect is smaller than your windows rectangle. This is a initial guess.

Form.Show() moves form position slightly

I have an application with a main form that acts as a sort of frame and an area inside the main form where I open other forms. These forms must not be closed ever so when a new one is opened I use this code to open it for the first time:
frm.WindowState = FormWindowState.Maximized;
frm.BringToFront();
And then if another form gets opened on top of that and I need to show it again I just use:
frm.Show();
The problem is when I open the form the first time its positioned perfectly and the borders line up nice. When I use frm.Show() to bring it back it moves it slightly to the left and down. Any clue why?
You should set the StartPosition property to 'manual' in form properties window
or
frm.StartPosition = System.Windows.Forms.FormStartPosition.Manual;
From your explanations, I understand you're using a parent form to contain MDI child forms (correct me if I'm wrong)
When a new MDI child form is shown, it is placed so that child forms are in "cascade", i.e. each child form is at the same position of the previous one plus an offset. When you hide a child form and show it again, the MDI container probably considers that it's a new child form, and it places accordingly...
I think you need to save the location of the child form before you hide it, so that you can restore it when you show it again

Categories