Andrew
Andrew

Reputation: 477

How to Lay Out a Form to permit Maximizing the window

I have a pretty simple form:

LAYOUT

Left Side of Form:  Label + Texbox on top, Listbox below
Right side of Form:  vb.net chart object
below chart:  start and end time datepicker + refresh button

All I really want to do is allow the form to be maximized and increase the chart size and listbox.

What is the best way to do something like this? When I used docking to the right, the chart overlapped the datetimepickers.

Upvotes: 0

Views: 572

Answers (1)

Jens
Jens

Reputation: 6375

As Hans said in his comment the Anchor property of the controls is important for what you are trying to do.

First layout your form in a size you like. Then click on the controls and find the Anchor property.

enter image description here

What this property does is it basically pins the controls to the selected edges of the Parent.
When the selected edge moves, the controls same edge moves the same way.

So if you pin something for example to all 4 edges it will maintain its distance to the forms sides on all sides. If you pin it only to some edges it will resize only with the selected sides.
If you pin a control for example neither left nor right it will stay at its relative position on the horizontal axis (e.g. centered).

To now achieve what you want you can assign the anchor points as follows:

enter image description here

The Label only moves with the top and left borders. It will stay where it is on resizing. The ListBox will not move but since it's pinned to the top and the bottom it will resize with the form in height.

The DateTimePickers are pinned not to the top but to the bottom. That way they will not resize but move down if the form changes its height and so on.

Experiment a bit with this to become familiar. If you continue to use WinForms it will be a very important asset to your skills.

One note: Don't be surprised about the ListBox's scaling. It scales only in full items (so there are never partial items visible) which makes it change its height in steps and not smoothly. Consider using a DataGridview or ListView instead.

Also be aware of AutoSize features that hinder free sizing of controls (the Label comes to mind).

Upvotes: 2

Related Questions