Docking
The Docking feature attaches a control to the edge of its container. When a control is docked to an edge of it's container, it will always stick to that edge. The control will also resize itself to fully fit the edge of the container and also resizes as the container is resized. The Dock property of the Form control can be set to any one of the six values, Top, Bottom, Left, Right, Fill, and None.
By default the Dock property is set to None. To graphically set the Dock property, click on the dropdown at the right of the Dock property in the Properties window. Then, select the edge to which you want to dock the control, or dock it to the center (Fill) or set to None.
Controls can also be docked by code:
' VB.NET
Me.Button1.Dock = System.Windows.Forms.DockStyle.Left
//C#
this.button1.Dock = System.Windows.Forms.DockStyle.Left;
![]() | |
| Figure 1: The controls here are named Buttons. Each button is docked to a different area of the form. |
If more than one control is docked to an edge, the controls will be placed next to each other. The second control is docked to the undocked edge of the first control so they're not placed on top of each other.
In Figure 1, Button1 and Button2 are docked to the Top edge of the container and placed next to each other. Similarly, Button3 and Button4 are docked to the Bottom. Button5 is docked to the Left edge of the Form, Button6 to the Right edge, and Button7 is set to Fill. This means that the control will resize itself to fill up the rest of the available area.
You can also dock the buttons as shown in Figure 1 using code:
Note: Controls, like the ComboBox, which has a fixed height, will not resize themselves vertically to fit the vertical edges of the container, if they are docked to Left, Right, or Fill.
' VB.NET
Me.Button1.Dock = System.Windows.Forms.DockStyle.Top
Me.Button2.Dock = System.Windows.Forms.DockStyle.Top
Me.Button3.Dock = System.Windows.Forms.DockStyle.Bottom
Me.Button4.Dock = System.Windows.Forms.DockStyle.Bottom
Me.Button5.Dock = System.Windows.Forms.DockStyle.Left
Me.Button6.Dock = System.Windows.Forms.DockStyle.Right
Me.Button7.Dock = System.Windows.Forms.DockStyle.Fill
//C#
this.button1.Dock = System.Windows.Forms.DockStyle.Top;
this.button2.Dock = System.Windows.Forms.DockStyle.Top;
this.button3.Dock = System.Windows.Forms.DockStyle.Bottom;
this.button4.Dock = System.Windows.Forms.DockStyle.Bottom;
this.button5.Dock = System.Windows.Forms.DockStyle.Left;
this.button6.Dock = System.Windows.Forms.DockStyle.Right;
this.button7.Dock = System.Windows.Forms.DockStyle.Fill;
Splitter Controls
The Splitter control resizes the docked controls at runtime. Place it between two controls and dock it to the undocked edge of one of them. The Splitter control is not visible at runtime, but when you point the mouse at the undocked edge of the control to which the splitter is docked, the cursor changes its appearance to indicate that the control can be resized.
Every Windows user has seen and used Windows Explorer to manage files and folders. It has two main components, a TreeView control on the left and a ListView control on the right. Resize these by dragging the vertical bar between them with the mouse in horizontal directions.
![]() | |
| Figure 2: This type of Windows Explorer user interface is made easily in .NET. |
You can create a similar interface without writing a single line of code (Figure 2).
An Outlook User Interface
Another classic user interface is the Outlook Express user interface. This is similar to designing the Windows explorer interface, except that there are two controls on the right, a ListView, and a RichTextBox control, both of which can be resized vertically.
![]() | |
| Figure 3: This is how your panels should look after following the previous steps. |
Now, your interface is ready.
Anchoring maintains a constant distance between the anchored edge of the control and the corresponding edge of the container. The Anchor property determines which edges of the control are anchored to the container's edges. When a particular edge of the control anchors to an edge of it's container, the edge always maintains the same distance even if the form is resized. However, unlike with the Dock property, the edge will not be positioned flush against the edge. The Anchor property of the form control can be set to any combination of the 4 values, Top, Bottom, Left, and Rightor it can be set to None.
![]() | |
| Figure 4: Controls can be anchored using the Properties window. |
By default, the Anchor property of a control is set to Top, Left, which means controls are always at a constant distance from the top and left edges of their containers.
Controls can be anchored using either the Properties window (Figure 4) or with code:
' VB.NET
Me.Button1.Anchor = _
(System.Windows.Forms.AnchorStyles.Bottom Or _
System.Windows.Forms.AnchorStyles.Right)
//C#
this.button1.Anchor =
(System.Windows.Forms.AnchorStyles.Bottom |
System.Windows.Forms.AnchorStyles.Right);
If a control is anchored to both Left and Right, or to both Top and Bottom, or to all four edges of the Form, resizes as its container doesin addition to anchoring itself. The result is that the control stretches or shrinks (Figure 5). Additionally, if a control's Anchor property is set to AnchorStyle.None, it repositions itself as the container resizes so that the distance to the edges remains proportional.
Note: Some controls, like ComboBox, which has a fixed height, will not resize/stretch/shrink themselves vertically even if they are anchored to the Top and Bottom edges.
![]() | |
| Figure 5: Start a new Windows application and place all the controls as shown. |
Now run through it. Start a new Windows application and place all the controls as shown in Figure 5. Note that:
Anchor the controls in the following manner:
In real-life programming, nothing is as simple as it seems. Form layouts are no exception. Learn about common pitfalls by designing this sample form layout of an expense tracker (Figure 6).
![]() | |
| Figure 6: The basic layout for the expense tracker form. |
Here are the requirements for your sample form:
Arrange the controls on the form as shown in Figure 7. Use the docking and anchoring features to make them resizable.
Notice that the controls either overlap, move away to opposite extremes of the form, or get jumbled up. If you set the Anchor property, the Dock property vanishes and vice versa. This is because you cannot set both Anchor and Dock properties for the same control, you can either anchor or dock a control but not both.
To solve this problem, use Panels to group controls that need to have identical behaviors. Dock the panels on the form as desired and anchor each control within its respective Panel. You'll need to use a little resizing code in the Form's resize event. This is definitely much easier than resizing controls in VB6. Most of the work is done by the Anchor and Dock properties with the help of Panels.
![]() | |
| Figure 7: Your real life sample form should look something like this. |
Now build the form shown in Figure 7.
![]() | |
| Figure 8: Here, the Yellow Panel is not resizing because it's docked to Left, and not set to Fill. |
The Yellow and Cyan Panels are not resizing vertically because the container of both these Panels (the Red Panel) is docked to Top and not set to Fill.
This is where a little code comes into play. Programmatically increase or decrease the width of the Yellow Panel in proportion to the change in the width of the form.
Put the following code in the Form's resize event:
To get the correct width of the Yellow Panel at runtime, multiply the width of the Form by a ratio calculated using the formula:
' VB.NET
Private Sub Form1_Resize(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Resize
pnlYellow.Size = New System.Drawing.Size(Me.Size.Width * _
0.4225, pnlYellow.Size.Height)
End Sub
//C#
private void Form1_Resize
(object sender, System.EventArgs e)
{
pnlYellow.Size = new System.Drawing.Size((int)
(this.Size.Width * 0.4225), pnlYellow.Size.Height ) ;
}
In this case the ratio is 0.4225, which means the width of the Yellow Panel is 42.25% of the width of the Form. Hence, at runtime multiply the current width of the Form by 0.4225 and set that value as the new width of the Yellow Panel in the Form's resize event. Now try it again:
Ratio = WidthOfYellowPanel / WidthOfTheForm
![]() | |
| Figure 9: The final version of the sample form. |
Note: You won't anchor the controls of the Yellow Panel to Top or Bottom, because the Yellow Panel will not resize vertically.
As you can see, Panels play a very important role in the layout design. And thankfully, the amount of coding needed to resize/reposition controls has been greatly reducedif not totally eliminatedin .NET.
| DevX is a division of Internet.com. © Copyright 2010 Internet.com. All Rights Reserved. Legal Notices |