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 Co开发者_开发技巧mboBox 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:
- When changing the size of the form, a width of both controls must be equal to the width of the form.
- The height of the TreeView must be changing to fill all free space of the form.
You can do like this:
- In the forms designer, layout the controls like you want them to look
- Select the ComboBox, and set the
Anchor
property to Top, Left and Right - 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.
精彩评论