Docked panels layout - c#

In winforms I am trying to setup 4 panels docked: top, left, right and middle. But when I dock the top and try to dock another panel on the left, that left panel overlays the top panel..Am I doing something wrong there? is there another way of docking several panels?

You may need to be careful to not drop the panels into any of the other panels before selecting their docking property, or they will assume that the panel they were dropped into is their parent.
I created the setup you see below by simply using 4 panels, each one dropped individually and then assigned the correct docking attribute. I made sure when I dropped them to drop them in space that was currently not used by any existing panels.
I'd assume this is what you are after, correct?

Related

How do I make the widths and lengths of an object automatically resize?

How do I make the widths and lengths of an object automatically resize?
What I mean by this is that I'm trying to make my design responsive. The problem is that when a user resizes my app, the objects don't fill the empty spaces that are created (E.g, a box has the same problem).. I'm using Visual Studio and I'm not sure if this is a property or a programming issue..
In winforms, controls have a property Dock. You can set it to Left, Right, Top, Bottom or Fill. If you set the Dock property for the various controls in your form, they will fill the available space whenever your form is resized.
Usually, the "main" control on your form will have a docking style of Fill, while the main menu toolbar will have Top, the status bar will have Bottom and so on. Play around with the Dock property until you get your form organized right.
You may also have controls that must keep a fixed size when their parent form is resized, but must align to one of the edges of the form. For this purpose, controls also have an Anchor property. Set it to Left, Right, Top or Bottom.

Make DataGridView stretch to middle of WinForms form?

I have a WinForm form that has two DataGridView controls paced on it such that they are stacked, one above the other against the right hand side of the form.
I would like a way of setting them so that when I expand the form, they expand height-wise with it, as well as width-wise. I managed width-wise by anchoring them to the left and right sides and anchoring the top one to the top and the bottom one to the bottom. However, from here I'm not sure how to get them to use up the space in the middle that appears when the form maximizes...Maybe an image will make my meaning clearer:
Normal Size:
Maxmized; I'd like the grids to expand to take up the full height of the form between the two of them as the red arrows show:
If this question is blindingly obvious I apologise and can only say I didn't really know how to phrase it properly and so found searching for it on Google unhelpful!
You have two options:
TableLayoutPanel or
SplitContainer
The former lets you create a table of many columns and/or rows with various sizing options from absolute and percent to autosize. This is very powerful for layout; but in other respects TLPs are somewhat restricted as the 'cells' are only virtual..
A SplitContainer offers only two panes but lets you treat each with all the things you can do to a container: add one or more controls, anchor or dock them, give each pane a BackColor and make use of its event model.
So if you need just two controls of equal size that adapt to the form size like you showed in the question, a SplitContainer is maybe the better option.
Set the splitter to fixed and make it smaller, anchor the SplitContainer to all sides and drop the DGVs into their panes and Dock them to Fill.
You could also make the splitter moveable to allow the user to resize the panes; if you do that do make the splitter width larger..
Also make sure that the FixedPanel is set to None so that height changes are shared.
Hint: If you want a few more panes to share the space you can nest several SplitContainers.. But for larger numbers do consider switching to TLP!

Prevent Windows form elements from leaving view when resizing from the bottom?

I have a form set up as shown in the picture below. The form simply contains a SplitContainer with two TreeViews in each of the panels, plus the label above the TreeView.
Here's a picture of the form that has not yet been resized:
Each TreeView is anchored to the Top and Left, and docked to the Bottom.
Each label is anchored to the Top and Left.
The SplitContainer is anchored to the Bottom, Left, and Right.
The above settings allow me to resize the TreeViews equally when the form is resized. However, when I resize the form's height, the labels (and of course the TreeViews as well) are pushed out of the form view, as shown in this picture:
How can I prevent the elements from being pushed past the title bar? Preferrably without the use of a SizeChanged event. I have tried adding padding to the form, as well as trying different docking/anchor modes, but I have been unable to restrict this.
Perform the following steps in order:
Either anchor the SplitContainer to all 4 sides or dock-fill it.
Place the labels on the panels. Make them smaller than the panels, in order to be sure that they really become child controls of the panels (and not of the form). Set their Dock property to Top.
Place the TreeViews on the panel. It is important that this is done after you placed and docked the labels! Same thing here: make them smaller than the panels. Then set their Dock property to Fill.
Note: you can either dock or anchor a control, but not both! The Anchor property is ignored if Dock is something else than None. An anchored edge keeps a constant distance to the side it is anchored to. If an edge is not anchored, the size of the control in this direction will remain unchanged. If two corresponding edges (e.g. left and right) are not anchored, then the control will be centered in this direction.
See: Manage WinForm controls using the Anchor and Dock properties
Set SplitContainer.Dock = DockStyle.Fill; if thats the only thing appearing on the form.

winform how to avoid two panel docking to be overlapped

Here is the picture to show the left panel is docked to left while the main panel is docked to Fill, but the left panel is overlapping the main panel
Any one know how to solve this problem?
You need to make sure there are on same level, play a little bit with:
Right Click on the control -> Send to Back or Bring to Front
options.
To give a bit more explanation:
If one of your panel is Dock to Left and the other to Bottom, but those are not on same level (Left panel is above Bottom one) Left panel will never force Bottom panel to move, as Bottom panel can use whole space, as on it's level there is nothing docked to Left it that makes sense.
Situation change if those two panels are on same level, then they respect each others docking.
In your scenario, you want to push your left hand side panel to back, so it is on same level with Main panel, and then act same way with the Bottom one, as needed.
I hope that helps.
The order in which the panels are created is causing this.
You can change this order with the document outline.
Set one panel higher in the list than the other, play with it until you get the desired result. Than close this window. In my case I also have to restart visual studio now since this view somehow makes visual studio very slow.
I have done the same thing but for me the two panels are not overlapping.
Let me tell you the steps I followed.
I took one panel from toolbox. Then made a copy of same panel by click and drag. I then changed their color two red and yellow respectively to differentiate between them. Then for red panel i set dock to left and for yellow panel I set dock to fill. Below is the screenshot for the same.
First case
Second Case
Third Case

Modify docking layout in WinForms

I have a SplitContainer on my form that has its Dock property set to Fill. It contains several child controls, many of which have event handlers attached to them. Later I decide to put a StatusStrip at the bottom of my form. Guess what, I can't set the StatusStrip to dock to the bottom of my form. The SplitContainer will continue to Fill the entire form. Even though the StatusStrip apparently gets docked to the bottom, it actually hides the bottom part of the SplitContainer behind it.
The only around it is to CUT the SplitContainer and then PASTE it back. Cutting the SplitContainer makes the StatusStrip the only control on my form and thus lets it capture the bottom docking. Afterwards, pasting the SplitContainer allows it to fill the remaining area. In short, docking uses First Come, First Serve method.
Now since my controls have lots of event handlers attached to them, cutting and pasting becomes a nightmare for me. Having my project in C# means I have to attach all those event handlers manually.
Is there a better work around?
This is a z-order issue between the splitter and the statusstrip. When you have a control you want to dock fill and one or more controls you want to dock top, left, right, or bottom, you have to have the fill control be the first in the z-order.
The better way is to open the Document Outline tool, select the SplitContainer and use the up or down buttons to change its z-order.
I should add that in Winforms the z-order is specified by the order in which you add controls to the Controls collection. That order determines the order the associated system controls are created, hence their z-order. Using the Document Outline tool to alter z-order simply causes the generated code to be re-ordered.

Categories