Resizing a child form controls when resizing a Parent - c#

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.

Related

How to autosize the form window based on screen resolution during run time (Where the size of the window created during design is too large)

I have created a windows application where the form size is large enough to view.
I want the form to shrink or maximize based on the screen resolution of the display such that the user can view all the controls in a compact way.
I tried the auto size property in the form but that doesn't work. Is there any property in the form which can resolve this issue?
Or do I need to code something else to resolve this issue ?
Thanks
Setting the WindowState = Maximized will cause the window to open to the full extents of the screen resolution. It doesn't matter what that value is, it will match it.
Edit:
From your comments, it sounds like you want what the AutoSize property will accomplish. I updated the form to add some controls and set the AutoSize = True and the AutoSizeMode = GrowAndShrink. Between these three properties, you should be able to get the form to do exactly what you wish. The one thing to pay attention to is the full extents of your controls within the form. From this picture you can see the form during runtime will resize to fit both text boxes while in the editor, I shrunk the form to hide almost everything. Please also note that in the example below, I set the WindowState = Normal.
Inside the form load event, do this:
private void Form_Load(object sender, EventArgs e) {
Rectangle resolutionRect = System.Windows.Forms.Screen.FromControl(this).Bounds;
if (this.Width >= resolutionRect.Width || this.Height >= resolutionRect.Height) {
this.WindowState = FormWindowState.Maximized;
}
}

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.

Resizing contents inside DevExpress Docking Panels

I am attempting to add three panels to a window using a Devexpress Docking manager and dockable panels. Here are the current results:
The three panels are placed and sized how I would like them however their contents will not correctly resize as I resize the window. This first image indicates this by the Picturebox that fails to fill the window. My current attempt to regulate this is: (Panel3 refers to a panel that contains pictureBox1. which in turn is contained by dp3.)
void dp3_SizeChanged(object sender, EventArgs e)
{
panel3.Size = panel3.Parent.Size;
pictureBox1.Width = dp3.Width;
pictureBox1.Height = dp3.Height;
}
The Same is true for the Controls Window. I have controls that do not appear unless the window is grossly oversized.
The controls are contained in 4 seperate panels that are themselves contained in the dockable window.
How do I make things appear the correct size and location whendocking and resizing?
Go throught this DevX article - Designing Resizable Windows Forms in
Visual Studio .NET-2,
that i like most for understanding about layout in Winforms.
You should set the Anchor and Dock properties on the controls in the forms.
The Anchor property controls which edges of a control are "bound" or "tied" to the corresponding edges of its form.
For example, if you set Anchor to Bottom, the distance between the control's bottom edge and the bottom of its parent will not change, so the control will move down as you resize the form.
If you set Anchor to Top | Bottom, the control will resize vertically as you resize the form.
To make a control resize with the form, set the Anchor to all four sides, or set Dock to Fill.
You can set the control's Dock property to Fill. This will cause the control to fill it's parent container.
You may still need to write some code to handle laying out the child controls. You can either do this by handling the Resize event, or by using a container that supports resizing for you (such as FlowLayoutPanel or TableLayoutPanel).
Use your Control's Anchor property. You'll probably need to set it to all sides, Top, Bottom, Left, Right, if you want it to resize according to parent control in all four directions
If you want to Maintain the controls Aspect Ratio on Resize, You'll need to store off the aspect ratio somehow, whether it's something known to you at design time or if you just want to calculate it in the constructor of the form after InitializeComponent(). In your form's Resize event,

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

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.

Categories