Controlling dock order The Dock Property

206 Anchor the OK and Cancel buttons on the top and right edges. btnOk.Anchor = AnchorStyles.Top Or AnchorStyles.Right btnCancel.Anchor = AnchorStyles.Top Or AnchorStyles.Right Anchor the Filename text box on the top, left, and right edges. This causes the text box to resize itself as needed. txtFilename.Anchor = AnchorStyles.Top Or AnchorStyles.Left _ Or AnchorStyles.Right

5.3.2 The Dock Property

The Dock property lets a control be docked to any one side of its container or lets it fill the container. Docking a control to one side of a container resembles laying out the control with three of its edges adjacent and anchored to the three corresponding edges of its container. Figur e 5- 8 shows a text box control docked to the left side of its form. Figure 5-8. A TreeView docked to the left side of a form The Dock property is defined by the Control class in the System.Windows.Forms namespace and so is inherited by all controls and forms. Its syntax is: Public Overridable Property Dock As System.Windows.Forms.DockStyle The DockStyle type is an enumeration that defines the values Left , Top , Right , Bottom , Fill , and None . Only one value can be used at a time. For example, a control cant be docked to both the left and right edges of its container. Setting the Dock property to DockStyle.Fill causes the control to expand to fill the space available in its container. If the control is the only docked control in the container, it expands to fill the container. If there are other docked controls in the container, the control expands to fill the space not occupied by the other docked controls. For example, in Figur e 5- 9 , the Dock property of textBox1 is set to DockStyle.Left , and the Dock property of textBox2 is set to DockStyle.Fill . Figure 5-9. A left-docked control and a fill-docked control

5.3.2.1 Controlling dock order

207 Controls have an intrinsic order within their container. This is known as their z-order pronounced zee order. The z-order of each control is unique within a given container. When two controls overlap in the same container, the control with the higher z-order eclipses the control with the lower z-order. Docking behavior is affected by z-order. When two or more controls are docked to the same side of a form, they are docked side by side. For example, the two text-box controls shown in Figur e 5- 10 are both docked left. Figure 5-10. Docking two controls to the same side of a form In such a situation, the relative position of each docked control is determined by its z-order. The control with the lowest z-order is positioned closest to the edge of the form. The control with the next higher z-order is placed next to that, and so on. In Figur e 5- 10 , textBox1 has the lower z-order, and textBox2 has the higher z-order. So how is z-order determined? In code, z-order is determined by the order in which controls are added to their containers Controls collection. The first control added through the collections Add method has the highest z-order; the last control added has the lowest z-order. For example, to produce the display shown in Figur e 5- 10 , textBox2 must be added to the Controls collection first, as shown here: Me.Controls.AddMe.textBox2 Me.Controls.AddMe.textBox1 This results in textBox1 having the lower z-order and therefore getting docked directly to the edge of the form. If the Controls collections AddRange method is used to add an array of controls to a container, the first control in the array has the highest z-order and the last control in the array has the lowest z-order. For example, textBox1 and textBox2 in Figur e 5- 10 could have been added to their container using this code: Assumes Imports System.Windows.Forms Me.Controls.AddRangeNew Control {Me.textBox2, Me.textBox1} After controls have been added to their containers Controls collection, you can change their z-order by calling the Control classs SendToBack or BringToFront methods. The SendToBack method gives the control the lowest z-order within its container causing it to dock closest to the edge of the form. The BringToFront method gives the control the highest z-order within its container causing it to dock furthest from the edge of the form. For example, the following code forces the controls to dock as shown in Figur e 5- 10 , regardless of the order in which they were added to the forms Controls collection: Me.textBox2.BringToFront The order in which the controls are instantiated in code and the order in which their respective Dock properties are set do not affect the controls z-orders. 208 When you design forms using Visual Studio .NETs Windows Forms Designer, the controls added most recently have the highest z-orders and therefore dock furthest from the edges of their containers. The z-orders of the controls can be changed within the designer by right-clicking on a control and choosing Bring to Front or Send to Back. When one of the controls in a container is fill-docked, that control should be last in the dock order i.e., it should have the highest z-order, so that it uses only the space that is not used by the other docked controls. Dock order also comes into play when controls are docked to adjacent sides of a container, as shown in Figur e 5- 11 . Figure 5-11. Docking two controls to adjacent sides of a form The control with the lowest z-order is docked first. In Figur e 5- 11 , textBox1 has the lowest z-order and so is docked fully to the left side of the form. textBox2 is then docked to what remains of the top of the form. If the z-order is reversed, the appearance is changed, as shown in Figur e 5- 12 . Figure 5-12. Reversing the dock order In Figur e 5- 12 , textBox2 has the lowest z-order and so is docked first, taking up the full length of the side to which it is docked the top. Then textBox1 is docked to what remains of the left side of the form. This behavior can be exploited to provide complex docking arrangements, such as the one shown in Figur e 5- 13 . Figure 5-13. Complex docking arrangements are easily created 209 The text boxes in Figur e 5 - 13 are in ascending z-order textBox1 is lowest; textBox6 is highest. The Dock properties of the controls are set like this: textBox1.Dock = DockStyle.Left textBox2.Dock = DockStyle.Top textBox3.Dock = DockStyle.Right textBox4.Dock = DockStyle.Bottom textBox5.Dock = DockStyle.Left textBox6.Dock = DockStyle.Fill

5.3.2.2 The Splitter control