Specify the location of a maximized form? - c#

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.

Related

How do I add a form to the panel properly

I designed a side panel and when I press the buttons, the form inside the panel on the right changes. However, the components in the forms I designed appear in the wrong places. Some are not visible because they are left outside.
I upload some photos. Design Screen and
at Runtime
Where am i doing wrong ? Panel Size and form size are same.
UPDATE:
I created usercontrol instead of form. However, nothing has changed. For example, when I put two labels side by side with 5 px between them in the design, it seems to have much more spacing if I run it. Or the labels I placed at the far right or at the bottom do not appear at runtime.
TelemetriUI telemetriUI = new TelemetriUI();
telemetriUI.TopLevel = false;
telemetriUI.FormBorderStyle = FormBorderStyle.None;
telemetriUI.Dock = DockStyle.Fill;
this.mainPanel.Controls.Add(telemetriUI);
this.mainPanel.Tag = telemetriUI;
telemetriUI.BringToFront();
telemetriUI.Show();

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.

Prevent Child Components when Overlapping Panel

I have a WinForms application which has two panels which individually contain a usercontrol component (one in each panel). How ever, because both panels are the same size and in the same location, the top most panel becomes a child of the other panel. I want to be able to hide one panel and then show the other panel:
this.panel1.visibile = false;
this.panel2.visibile = true;
But when I hide panel1, the second panel is hidden as well.
How can I make panel2 a non-child of panel1?
I like to keep things simple because I'm new to C# Programming.
This is a common accident in the designer. Dropping the second panel on top of the first one will make it a child of the first panel. Two basic strategies to fix this:
View + Other Windows + Document Outline. Click the second panel in the list and drag it to the form. You'll need to fix up the Location property by hand by typing its value in the Property window.
Keep the upper left corner of the second panel just a bit to the left or top of the first panel. Put it in the right place in the form's constructor by assigning its Location property after the InitializeComponent() call.
Check this answer for a control that works well at design time and lets you easily flip panels at runtime.
The designer will do this automatically because it assumes that when you drag one control over another, you want to make it a child of that control.
What I usually do to get around this is to create the control in a different place on the form, and then use the properties to manually match the positions of sizes of the two controls.

Custom anchoring in Windows Forms

I have a custom form that has 4 panels on it edges. I would like to anchor another panel to contain user controls to the visible edges of the form. Once that is done I would then like to anchor an undetermined number of controls to the already anchored panel. I am rather new to this and I do not know that this is the best way to achieve my goal of having a list of objects that resize as the form is resized. The reason I am working at it from this angle is I want the objects to be clickable, and moveable, not just text lines.
My thought is something like this.
----------------------------------------
| Header Panel |
----------------------------------------
|| <--left panel right panel->||
|| [ user control object ]||
|| [ user control object ]||
|| [ user control object ]||
|| [ user control object ]||
----------------------------------------
| Bottom Panel |
----------------------------------------
Currently I am able to draw the panel that holds the user control objects as desired, but it does not resize with the parent, and when I try to anchor it to the parent, it anchors to the wrong place. I have also attempted to anchor the user control objects to the user control panel but they are not resizing at all.
So here is the code for the container panel
//This is called after InitializeComponent(), I would assume the anchor would go
//in here somewhere, but I need the anchor to be offset by the bounds of the
//other panels as listed below.
private static void SetQuestionContainerBounds(SessionReviewForm instance)
{
instance.pnlQuestionContainer.Top = instance.HeaderPanel.Bottom;
instance.pnlQuestionContainer.Left = instance.LeftPanel.Right;
instance.pnlQuestionContainer.Width = instance.RightPanel.Left - instance.pnlQuestionContainer.Left;
instance.pnlQuestionContainer.Height = instance.StatusPanel.Top - instance.pnlQuestionContainer.Top;
}
After I make the form and position the pnlQuestionContainer, I then start making user controls called base question objects, anchor them, and added them to the pnlQuestionContainer
private void DisplayData()
{
// tracks the number of questions, used in placement of objects
int questionCount = 0;
// if the session question is marked for review
// generate a new question object and place it.
foreach (SessionQuestion sq in thisSessionPart.SessionQuestions)
{
if(sq.MarkForReview)
{
BaseQuestionObject bqo = new BaseQuestionObject(sq, parentSession);
BaseQuestionObject.FitAndPlaceObject(pnlQuestionContainer, bqo, questionCount);
bqo.Anchor = (AnchorStyles.Left | AnchorStyles.Right);
pnlQuestionContainer.Controls.Add(bqo);
questionCount++;
}
}
}
A base question object is made up of three parts at the moment. A userControl, a group box, and a label. All of these items are set to autosize with anchor of left, right with the exception of the user control as I cant set that in the properties window, but I think i am setting it in the above method.
The current results are that the container panel is drawn perfectly at first, but it never resizes. The question objects are drawn at the same size that they were made, though this is not the max or the min size.
[EDIT]
The issue I had was the anchor styles were not playing nice with autosize. After turning autosize off and manipulating the anchorStyles I was able to get the desired results.
First, I'm not 100% sure of what you are trying to do but anchoring is much simpler than the way you are trying to achieve it.
Each control has a property called Anchor which is an AnchorStyles enumeration. These values can be Left, Right, Top, Bottom or None and can be bitwised ORed together to allow for multiple anchoring values.
The default 'Anchor' value is Top-Left. If you change the anchoring to Top-Right your controls will remain the same size, but will "float" with the right-side of your application window as it is resized. If you anchor to the Left, Top and Right, your controls will grow and shrink as you resize the width of your window.
I do not believe that you should need any of these panels, unless they are used to logically group controls together. You can experiment with the anchoring by placing a bunch of controls on a form and changing their anchor settings. Run your test form and resize it and see what happens.
Additionally, if you would like to re-arrange the contents of your form as the Window grows and shrinks, the standard MS toolbox has a few controls that you can play around with. Specifically, check out the FlowLayoutPanel and TableLayoutPanel. You can use these controls to fine tune how you want to reposition your child controls on a form or in a section of a form.

Best way to make Windows Forms forms resizable

I am working on a largish C# project with a lot of Windows Forms forms that, even though you can resize the form, the elements in the form don't scale.
How can I make the form elements (such as the datagridview, text area's, etc.) scale when the user changes the size of the form?
Nearly all the forms subclass from one specific form, so if there's something I can do in the base class, that'd be great.
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.
Use the Anchor and Dock properties.
Anchor allows you to pin specific sides of the control to the sides of the parent control.
Dock will bind the whole control to a side of the parent control or it can be set to fill the contents of the parent control.
You usually just need to set the Anchor to the bottom and right of the parent control but gets more difficult when you have controls side by side, then you need to manually resize the controls on the forms OnResize event to get them to scale naturally together.

Categories