Anchoring and docking in windows forms




















If one child control changes in size, the other controls docked next to that control move along with it.

Unlike the Anchor property, you can set the Dock property to only a single value. The valid values are shown below:. To set the Dock value of a control, select the control in Visual Studio and go to the properties window. You will see a property labeled "Dock". Clicking in the value section for this property will bring up a little window that allows you to specify how you would like the control to be docked.

This form is shown in the following images Figures E, F, and G with various values assigned:. Like the Anchor property, none of this makes a lot of sense until you see it in action. Figure H shows a form with five child controls, all set with different dock values. Figure I shows the same window as Figure H , except now the window has been resized to a larger footprint.

Figure J again shows the same window as Figure H , except this time the controls on the bottom, top, left, and right of the form have been made smaller. Notice that the control in the middle, which is set to dock Fill, automatically grew in size. One thing to keep in mind with the Dock property is that the order in which the controls are added to the form can affect the way that they dock. For instance, if you add ControlA to the form, instruct it to dock Fill, then you add ControlB to the form and instruct it to dock Top, ControlB will appear to cover up the top portion of ControlA.

This will bring ControlA to the front of ControlB and the controls will then act as expected. To fully understand how to use Dock and Anchor properties, download the sample application that is included with the download version of this article. There are several forms within the application that demonstrate different uses of Dock and Anchor.

The default is to anchor the control to the Top, Left edge of the form. Click the arrow to reveal a curious drop down box:. The button in the middle represents your control. The big white areas are rather confusing - they don't actually do anything! To change the property, you click the smaller grey or white rectangles between the big white rectangle. Click again to deselect it. In the image below, the property has been changed so that the textbox is anchored to the Top, Left and Right sides of the form:.

Notice where the cursor is in the images, and what has been changed. Click the arrow on the drop down box to confirm your choices.

For example, Windows Explorer docks its TreeView control to the left side of the window and its ListView control to the right side of the window. One of the key methods of control for dock is to remove it before it's had a chance to flower and set seed. In beds and borders carefully dig up dock using a fork to loosen the soil and avoid breaking the taproot. Every bit of the root needs to be removed to prevent new weeds growing from the fragments.

Organic mulch. Docking refers to how much space you want the control to take up on the form. If you dock a control to the left of the form , it will stretch itself to the height of the form , but its width will stay the same.

How do you kill a dock? How to Kill Dock Weed Dig out the dock weed and its taproot. Apply a broadleaf herbicide to control large infestations of dock weed. Use an organic herbicide, such as an insecticidal soap, to kill dock weed and other broadleaf weeds. This is the general rule for anchoring and docking in the FlowLayoutPanel control: for vertical flow directions, the FlowLayoutPanel control calculates the width of an implied column from the widest child control in the column.

All other controls in this column with Anchor or Dock properties are aligned or stretched to fit this implied column. The behavior works in a similar way for horizontal flow directions. The FlowLayoutPanel control calculates the height of an implied row from the tallest child control in the row, and all docked or anchored child controls in this row are aligned or sized to fit the implied row.

The following illustration shows four buttons that are anchored and docked relative to the blue button in a FlowLayoutPanel. The FlowDirection is TopDown. The following code example demonstrates various Anchor property values for a Button control in a FlowLayoutPanel control.



0コメント

  • 1000 / 1000