Anchoring in Windows Forms


System.Winforms.Anchorstyle Enumeration:

Specifies how a control anchors to the edges of its container. When a control is anchored to an edge of its container, the distance between the control and the specified edge remains constant when the container resizes. For example, if a control is anchored to the right edge of its container, the distance between the right edge of the control and the right edge of the container remains constant when the container resizes. A control can be anchored to any combination control edges. If the control is anchored to opposite edges of its container (for example, to the top and bottom), it resizes when the container resizes. If a control has its Anchor property set to AnchorStyle. None, the control moves half of the distance that the container of the control is resized. For example, if a Button has its Anchor property set to AnchorStyle.None and the Form that the control is located on is resized by 20 pixels in either direction, the button will be moved 10 pixels in both directions.

Members of System.Winforms.Anchorstyle Enumeration:

  • All: Each edge of the control anchors to the corresponding edge of its container.
  • Bottom: The control is anchored to the bottom edge of its container.
  • BottomLeft: The control is anchored to the bottom and left edges of its container.
  • BottomLeftRight: The control is anchored to the bottom, left, and right edges of its container.
  • BottomRight: The control is anchored to the bottom and right edges of its container.
  • Left: The control is anchored to the left edge of its container.
  • LeftRight: The control is anchored to the left and right edges of its container.
  • None: The control is not anchored to any edges of its container.
  • Right: The control is anchored to the right edge of its container.
  • Top: The control is anchored to the top edge of its container.
  • TopBottom: The control is anchored to the top and bottom edges of its container.
  • TopBottomLeft: The control is anchored to the top, left, and bottom edges of its container.
  • TopBottomRight: The control is anchored to the top, right, and bottom edges of its container.
  • TopLeft: The control is anchored to the top and left edges of its container.
  • TopLeftRight: The control is anchored to the left, top, and right edges of its container.
  • TopRight: The control is anchored to the top and right edges of its container.

Wondering where It would be useful?

Let us say you have a form and you have some controls on them. Now resize the window. The window resizes fine but the controls would stay where they are? Wouldn't it be nice if the controls flowed with the form, automatically resizing and repositioning themselves to  utilize all the space available to them? Let us say I have a tree view control and few buttons on the form.Let say the text in one of the nodes is vey long,would it not be nice if the treeview control expand with the form? You can achieve this by just setting anchor property.

How to Run ?

The attached code demonstartes each of the members of Anchorstyle enumeration. Run the code by running the app with each property at a time. Here is that section of the code written in  the constructor.

//PLAY WITH DIFFERENT VALUES OF ANCHOR
//button1.Anchor = System.WinForms.AnchorStyles.None;
button1.Anchor = System.WinForms.AnchorStyles.All;
//button1.Anchor = System.WinForms.AnchorStyles.Bottom;
//button1.Anchor = System.WinForms.AnchorStyles.BottomLeft;
//button1.Anchor = System.WinForms.AnchorStyles.BottomLeftRight;
//button1.Anchor = System.WinForms.AnchorStyles.BottomRight;
//button1.Anchor = System.WinForms.AnchorStyles.Left;
//button1.Anchor = System.WinForms.AnchorStyles.LeftRight;
//button1.Anchor = System.WinForms.AnchorStyles.Right;
//button1.Anchor = System.WinForms.AnchorStyles.Top;
//button1.Anchor = System.WinForms.AnchorStyles.TopBottom;
//button1.Anchor = System.WinForms.AnchorStyles.TopBottomLeft;
//button1.Anchor = System.WinForms.AnchorStyles.TopBottomRight;
//button1.Anchor = System.WinForms.AnchorStyles.TopLeft;
//button1.Anchor = System.WinForms.AnchorStyles.TopLeftRight;
//button1.Anchor = System.WinForms.AnchorStyles.TopRight;

Requirement:

.Net SDK

How to Compile?

csc /r:System.dll /r:System.winforms.dll /r:System.drawing.dll
/r:Microsoft.win32.interop.dll Anchor.cs

Source Code:

namespace Anchor
{
using System;
using System.WinForms;
/// <summary>
/// Summary description for AnchorForm.
/// </summary>
public class AnchorForm : Form
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components;
private System.WinForms.Button button1;
public AnchorForm()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
//PLAY WITH DIFFERENT VALUES OF ANCHOR
//button1.Anchor = System.WinForms.AnchorStyles.None;
button1.Anchor = System.WinForms.AnchorStyles.All;
//button1.Anchor = System.WinForms.AnchorStyles.Bottom;
//button1.Anchor = System.WinForms.AnchorStyles.BottomLeft;
//button1.Anchor = System.WinForms.AnchorStyles.BottomLeftRight;
//button1.Anchor = System.WinForms.AnchorStyles.BottomRight;
//button1.Anchor = System.WinForms.AnchorStyles.Left;
//button1.Anchor = System.WinForms.AnchorStyles.LeftRight;
//button1.Anchor = System.WinForms.AnchorStyles.Right;
//button1.Anchor = System.WinForms.AnchorStyles.Top;
//button1.Anchor = System.WinForms.AnchorStyles.TopBottom;
//button1.Anchor = System.WinForms.AnchorStyles.TopBottomLeft;
//button1.Anchor = System.WinForms.AnchorStyles.TopBottomRight;
//button1.Anchor = System.WinForms.AnchorStyles.TopLeft;
//button1.Anchor = System.WinForms.AnchorStyles.TopLeftRight;
//button1.Anchor = System.WinForms.AnchorStyles.TopRight;
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
public override void Dispose()
{
base.Dispose();
components.Dispose();
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container ();
this.button1 = new System.WinForms.Button ();
//@this.TrayHeight = 0;
//@this.TrayLargeIcon = false;
//@this.TrayAutoArrange = true;
button1.Location = new System.Drawing.Point (112, 104);
button1.ForeColor = System.Drawing.SystemColors.ControlText;
button1.Size =
new System.Drawing.Size (75, 23);
button1.TabIndex = 0;
button1.Anchor = System.WinForms.AnchorStyles.None;
button1.Text = "button1";
this.Text = "AnchorForm";
this.AutoScaleBaseSize = new System.Drawing.Size (5, 13);
this.Click += new System.EventHandler (this.Form1_Click);
this.Controls.Add (this.button1);
}
protected void Form1_Click (object sender, System.EventArgs e)
{
}
/// <summary>
/// The main entry point for the application.
/// </summary>
public static void Main(string[] args)
{
Application.Run(
new AnchorForm());
}
}
}


Similar Articles