Browse DevX
Sign up for e-mail newsletters from DevX


Designing Resizable Windows Forms in Visual Studio .NET : Page 4

Are you sick of wasting time writing resizing code for your Windows forms? .NET's new anchoring and docking properties enable form controls to automatically resize or reposition themselves as the form resizes. This article explains how to create resizable Windows forms automatically, or with just a few lines of code.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

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:

  • All TextBoxes and ComboBoxes should resize horizontally.
  • The multiline TextBox 'Remarks' should resize both horizontally and vertically.
  • The buttons 'Save' and 'Cancel' should remain at the Bottom Right corner of the window.
  • The label 'Remarks' should reposition itself so that it is always aligned to the middle of the TextBox.

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.

  1. Start a new Windows application and place two Panels on the form.
  2. Change the BackColor of the Panels to Red and Green so they are easily differentiated.
  3. Dock the Red Panel to the Top and adjust the height as necessary to accommodate the controls.
  4. Dock the Green Panel to fill up rest of the area.
  5. Place two more Panels on top of the Red Panel and change the BackColor of the Panels to Yellow and Cyan.
  6. Dock the Yellow Panel to the Left and adjust the width as necessary to accommodate the controls.
  7. Dock the Cyan Panel to fill up rest of the area on the Red Panel.
  8. It is important you do the docking in the order mentioned above or you will not get the desired layout.
  9. Run the application. The panels should resize as you resize the form.

Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date