Home > DeveloperSection > Articles > Dynamically Resizing Controls on Windows Forms – Using Anchor Property

Dynamically Resizing Controls on Windows Forms – Using Anchor Property


C# C# 
Ratings:
0 Comment(s)
 6736  View(s)
Rate this:

Dynamically Resizing Controls on Windows Forms – Using Anchor Property

Introduction

In this article let’s discuss how to dynamically resizing the controls on the Form taking leverage of Anchor Properties in Windows Application.

During our initial stages of Developing Windows application we might have faced this issue of dynamically resizing and positioning the controls on the Form. Frankly, speaking even when I was set off to develop my first application I have faced this Issue. In my understanding of Anchor property here I present this article to you. Any Suggestions or err in this Please suggest me.

Hope this article which I present here might be useful to Novice application developers.

Anchor Property

 

Anchor Property determines how a control is automatically resized when its parent control is resized. It defines the position for the control on the Form.

When designing a Form, that can be resized at run-time, the controls on the Form should resize and reposition accordingly. To achieve this we take the advantage of Anchor property.

Let us design a Form that can be resized at run-time and get to Understand about the Anchor property of controls.

Properties Window

To set the Anchor property for a Control from the Properties Windows we set it Anchor property.

Dynamically Resizing Controls on Windows Forms – Using Anchor Property

Here we have set the button1 button controls Anchor property to the bottom right corner of the Form.

Programmatically Setting Anchor Property

The Anchor property is set programmatically with a bitwise combination of  Anchor Styles values. The default is Top and Left.

 button1.Anchor = (AnchorStyles.Bottom | AnchorStyles.Right);

 

The combination of AnchorStyles.Bottom | AnchorStyles.Right anchors the button to the bottom right corner of the Form.

Now let us look through how it works!!

Dynamically Resizing Controls on Windows Forms – Using Anchor Property

Set the button1 Anchor property to Bottom, Right from the properties Window. Now when you resize your form by dragging the right bottom of the corner you can view a change indicating that the button is maintains the same distance from the bottom of the Form as well as to its right.

Creating our Resizable Form

      1.    Open Visual Studio and Select Windows Forms Application from the list of available Templates and name it DynamicallyPositioningControls.

 

Dynamically Resizing Controls on Windows Forms – Using Anchor Property

      2 Rename your Form to frmDynamicResizing by setting its Name property and sets its Text property to Dynamic Resizing Form.

      3. Now let us design our Form with controls and set its Anchor properties.

 

Dynamically Resizing Controls on Windows Forms – Using Anchor Property

 

Here we have used controls MenuStrip, Label, ComboBox, Button, DataGridView, TextBox and LinkLabel.

Anchor Properties for Controls used

Control Name

Anchor Property Value

cmb_Search

Top, Left, Right

bt_Search

Top, Right

bt_Add

Top, Right

bt_Delete

Top, Right

bt_Edit

Top, Right

bt_Close

Bottom, Right

lnk_systemInfo

Bottom, Right

grp_UserInfo

Top, Bottom, Left, Right

dg_UserInfo

Dock Property - Fill

 

The cmb_Search Anchor property (Top, Left, and Right) means that the ComboBox is anchored to Top Left corner of the Form and AnchorStyle Right indicates that the ComboBox is stretched to maintain the anchored distance to the top edges of the Form as the height of the Form is resized.

The bt_Search Anchor property (Top, Right) indicates that the control is anchored to top and right edges of the Form.

 

The bt_Close anchor the button to the bottom right corner of the form.

 

dg_UserInfo - The Anchor and Dock properties are contradictory. Only one can be set at a time, and the last one set takes priority.

 

We fix the minimum size of the Form to 517, 347 by setting its Minimum size.

Now we will run the Form and see how the UI resizes accordingly,

Dynamically Resizing Controls on Windows Forms – Using Anchor Property

After resizing the Form,

Dynamically Resizing Controls on Windows Forms – Using Anchor Property

Clicking the Maximize Button,

Dynamically Resizing Controls on Windows Forms – Using Anchor Property

Roundup

In this article we have discussed how to create a resizable UI (User Interface) by setting its Anchor property of the Forms. Anchor Property in Windows Forms help in creating resizable UI’s.

Thanks for Reading.


Don't want to miss updates? Please click the below button!

Follow MindStick