-1

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.

Nissa
  • 4,636
  • 8
  • 29
  • 37
DK7195
  • 3
  • 1

2 Answers2

1

Perform the following steps in order:

  1. Either anchor the SplitContainer to all 4 sides or dock-fill it.
  2. 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.
  3. 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

Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188
0

Set SplitContainer.Dock = DockStyle.Fill; if thats the only thing appearing on the form.

Scott
  • 180
  • 6
  • This did not prevent the issue. I am having the same issue with the labels are prevented from moving above the title bar, but not the TreeViews. – DK7195 Feb 21 '17 at 18:48
  • Your SplitContainer & views... place a Panel on EACH side of the SplitContainer - and dock these to the TOP. Adjust height as required, and place your labels containing the titles on these panels. With the panels being "docked" to the top, you can now dock fill your treeviews to each splitcontainer sides. Now with SplitContainer.Dock = Fill, this should work. If the labels are outside of the splitContainer then yes - you will have issues - doing the above will simply move the titles inside in the SplitContainer-but more importantly - using the panel, allows the treeviews to dock correctly. – Scott Feb 21 '17 at 22:06