简体   繁体   中英

How can I organize controls on a form relative to one another and the form itself?

I have a child form in a MDI Windows Forms application. It has two controls: a ComboBox and a TreeView, with the last one under the first one. Both controls have the same width. How can I set up them and form properties to achieve the following:

  1. When changing the size of the form, a width of both controls must be equal to the width of the form.
  2. The height of the TreeView must be changing to fill all free space of the form.

You can do like this:

  1. In the forms designer, layout the controls like you want them to look
  2. Select the ComboBox, and set the Anchor property to Top, Left and Right
  3. Select the TreeView and set the Anchor property to Top, Left, Right, and Bottom

Basically, you would need to dock your controls. Play with the Dock property of your both controls to find the "docking" which suits your requirements.

Here is an example which demonstrates a Combobox with Dock=Top and TreeView with Dock=Fill :

在此输入图像描述

If you resize the form, the Combobox width and TreeView width/height will be resized accordingly, which suits your specific requirements.

This is done by the Anchor property. Set it properly on all controls (combobox, treeview and usercontrol) and it will stretch however way you like.

The Dock property is similar, but it also affects location and kinda "glues" the control to its place even in the form designer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM