Home

Introduction to Controls Properties

   

Control Identification

 

Introduction

Most of the controls you will use to create your applications are defined in the .NET Framework and each is based on a particular class. To provide them with basic and common characteristics, all visual Windows controls of the .NET Framework are based on a class called Control which is available in the System.Windows.Forms namespace of the System.Windows.Forms.dll assembly. Based on this, the characteristics common to .NET Framework graphical controls are accessed and managed from one point, then inherited by those controls.

As you should know from your learning C#, in order to use a variable in your application, you must declare a variable for it and, when declaring a variable, you must give it a name. This rule also applies to Windows controls you will use in your application. Based on this, and as seen in the previous lesson, you can programmatically create a control by declaring a variable for and give that variable a name.

Control's Names

To create a control, the primary piece of information you must provide is its name. You do this when declaring a variable for the control. Here is an example:

public class Exercise : Form
{
    private Button btnSubmit;

    private void InitializeComponent()
    {
        btnSubmit = new Button();

        this.Controls.Add(btnSubmit);
    }
}

ApplicationTopic Applied: Identifying a Control

  1. Start Notepad
  2. Type the following:
    using System;
    using System.Windows.Forms;
    
    public class Controls1 : Form
    {
        private Button btnSubmit;
    
        public Controls1()
        {
            btnSubmit = new Button();
            Controls.Add(btnSubmit);
        }
    
        public static int Main()
        {
            Application.Run(new Controls1());
            return 0;
        }
    }
    
  3. To save the file, on the main menu, click File -> Save
  4. Locate the Exercise1 folder from the root of the C: drive and display it in the top combo box
  5. Change the name of the file to "Controls1.cs"
  6. Click Save
  7. To launch the Command Prompt, click Start
  8. In the text box, type cmd and Press Enter
  9. To switch to the root directory, type CD\ and press Enter
  10. To switch to the folder that contains the file, type CD Controls1 and press Enter
  11. To build the project:
    • If you have admin rights on the computer and if you had changed the path in the Environment Variables, type the following:
      csc /r:System.Windows.Forms.dll /target:winexe Controls1.cs
    • If you don't have admin rights on the computer and you had not changed the path in the Environment Variables, type the following:
      C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\csc /r:System.Windows.Forms.dll /target:winexe Controls1.cs
  12. Press Enter
  13. To execute, type Controls1 and press Enter
  14. To close the form, click its system Close button

A Control's Handle

The Win32 library uses a concept called handle to represent each object of an application. A handle in Win32 is simply an unsigned integer used in place of a control. You have a choice of using it or not but it exists. The handle is created by the operating system when the application comes up. This means that you don't need to create it but you can access it to retrieve its value.

To access the handle to a control, you can call its Handle property. In the .NET Framework, the Handle property is defined as an IntPtr value. To retrieve the handle of a control, you can access its Handle property.

The Textual Characteristics of a Control

 

Introduction

Some controls are text-based, meaning they are meant to display or sometimes request text from the user. If a control displays text, then it has a property called Text. Here is an example:

public class Exercise : Form
{
    private Button btnSubmit;

    private void InitializeComponent()
    {
        btnSubmit = new Button();
        btnSubmit.Text = "Submit";

        this.Controls.Add(btnSubmit);
    }
}

ApplicationTopic Applied: Identifying a Control

  1. In Notepad, change the file as follows:
    using System;
    using System.Windows.Forms;
    
    public class Controls1 : Form
    {
        private Button btnSubmit;
    
        public Controls1()
        {
            btnSubmit = new Button();
    	
    	btnSubmit.Text = btnSubmit.Handle.ToString();
    
    	Text = "Handle = " + Handle.ToString();
            Controls.Add(btnSubmit);
        }
    
        public static int Main()
        {
            Application.Run(new Controls1());
            return 0;
        }
    }
  2. To save the file, on the main menu, click File -> Save
  3. Return to the Command Prompt
  4. Press the up arrow key to make sure the command that builds the project is selected
  5. Press Enter
  6. To execute, type Controls1 and press Enter
     
    The Handle of a Control
  7. To close the form, click its system Close button

Auto-Sizing a Control

Many controls (not all), such as the Label, are configured to resize themselves as you provide a string to their Text property. If you type a long string, the control would be made large enough to show the whole text. Some other controls, such as the Button, will not resize themselves if their text is too long. 

The ability of a control to enlarge itself to accommodate its string is controlled by a Boolean property named AutoSize. For some controls, the default value of this property is False. For some other controls, this property is defaulted to True. Therefore, use this property to control whether a control on the form can be resized or not.

To specify the auto-sizing option of a control, access its AutoSize property and assign it the desired Boolean value. Here is an example:

public class Exercise : Form
{
    private Button btnSubmit;

    private void InitializeComponent()
    {
        btnSubmit = new Button();
        btnSubmit.Text = "Submit";
        btnSubmit.AutoSize = true;

        Controls.Add(btnSubmit);
    }
}

ApplicationTopic Applied: Auto-Sizing a Control

  1. In Notepad, change the file as follows:
    using System;
    using System.Windows.Forms;
    
    public class Controls1 : Form
    {
        private Button btnSubmit;
    
        public Controls1()
        {
            btnSubmit = new Button();
    	
    	btnSubmit.Text = "Handle = " + btnSubmit.Handle.ToString();
            btnSubmit.AutoSize = true;
    
    	Text = "Handle = " + Handle.ToString();
            Controls.Add(btnSubmit);
        }
    
        public static int Main()
        {
            Application.Run(new Controls1());
            return 0;
        }
    }
  2. To save the file, on the main menu, click File -> Save
  3. Return to the Command Prompt
  4. Press the up arrow key to make sure the command that builds the project is selected
  5. Press Enter
  6. To execute, type Controls1 and press Enter
     
    Auto-Sizing a Control
  7. To close the form, click its system Close button

The Location of a Control

 

Introduction to the Location

After adding a control to a window, the control is positioned in the body of the parent using a Cartesian coordinate system whose origin is located on the top-left corner of the parent window. The position of a control on a form (or a container) is referred to as its location. The location is defined by two values:

  • The horizontal value of a location is the distance from the left border of a form (or container) to the left border of a control
  • The vertical value of a location is the distance from the top border of a form (or container) to the top border of a control

The location of a Windows control can be illustrated as follows:

Location

The location of a control is specified using a structure named Point. To identify the concept of a point, the System.Drawing namespace provides the Point structure. The Point structure is defined in the System.Drawing.dll.

One of the properties of the Point structure is X, which represents the horizontal distance of the point from the top-left corner of the object that owns the point. Another property, Y, represents the vertical measurement of the point with regards to the top-left corner of the object that owns the point. A Point object can be represented on the Windows coordinate system as follows:

Representation of a Point

Locating a Control

You can specify the location of a control. To support this, each control has a property named Location. To assist you with specifying the location of a control, the Point class provides a constructor as follows:

public Point(int x, int y)

This constructor takes a left and a top arguments. Here is an example of setting the location of a control:

using System;
using System.Drawing;
using System.Windows.Forms;

public class Exercise : Form
{
    private Button btnSubmit;

    private void InitializeComponent()
    {
        btnSubmit = new Button();
        btnSubmit.Text = "Submit";
        btnSubmit.Location = new Point(100, 40);

        this.Controls.Add(btnSubmit);
    }

    public Exercise()
    {
        InitializeComponent();
    }
}

public class Program
{
    static int Main()
    {
        Application.Run(new Exercise());
        return 0;
    }
}

You can also use an existing Point object and assign it to the Location property of the control. Here is an example:

public class Exercise : Form
{
    private Button btnSubmit;

    private void InitializeComponent()
    {
        btnSubmit = new Button();
        btnSubmit.Text = "Submit";

        Point pt = new Point();
        pt.X = 100;
        pt.Y =  40;
        btnSubmit.Location = pt;

        this.Controls.Add(btnSubmit);
    }
}

You can also retrieve the location of a control and store it in a Point object. To do this, you can simply assign the Location property of a control to a Point object. 

public class Exercise : Form
{
    private Button btnSubmit;
    
    private void InitializeComponent()
    {
        btnSubmit = new Button();
        btnSubmit.Text = "Submit";
        btnSubmit.Location = new Point(100, 40);

        Point pt = btnSubmit.Location;

        Controls.Add(btnSubmit);
    }
}

When studying control alignment, we saw that you could use the location of one control as a reference to position another control. You can also do this programmatically by retrieving the location of one control and changing either its X or its Y values to position the new control. Here is an example:

using System;
using System.Drawing;
using System.Windows.Forms;

public class Exercise : Form
{
    private Button btnSubmit;
    private TextBox txtEmployeeName;

    private void InitializeComponent()
    {
        btnSubmit = new Button();
        btnSubmit.Text = "Submit";
        btnSubmit.Location = new Point(100, 40);

        txtEmployeeName = new TextBox();
        Point pt = btnSubmit.Location;

        txtEmployeeName.Location = new Point(pt.X, pt.Y + 32);

        Controls.Add(btnSubmit);
        Controls.Add(txtEmployeeName);
    }

    public Exercise()
    {
        InitializeComponent();
    }
}

public class Program
{
    static int Main()
    {
        Application.Run(new Exercise());
        return 0;
    }
}

The Point structure provides other constructors you can use for the location of a control.

ApplicationTopic Applied: Locating a Control

  1. On the main menu of Notepad, click File -> New
  2. Type the following:
    using System;
    using System.Drawing;
    using System.Windows.Forms;
    
    public class Controls2 : Form
    {
        private Button btnSubmit;
    
        public Controls2()
        {
    	InitializeComponent();
        }
    
        private void InitializeComponent()
        {
            btnSubmit = new Button();
            btnSubmit.Text = "Submit";
            btnSubmit.Location = new Point(12, 12);
    
            Controls.Add(btnSubmit);
        }
    }
    
    public class Program
    {
        static int Main()
        {
            Application.Run(new Controls2());
            return 0;
        }
    }
  3. To save the file, on the main menu, click File -> Save
  4. Locate the Exercise1 folder from the root of the C: drive and display it in the top combo box
  5. Change the name of the file to "Controls2.cs"
  6. Click Save
  7. Return to the Command Prompt
  8. To build the project:
    • If you have admin rights on the computer and if you had changed the path in the Environment Variables, type the following:
      csc /r:System.dll /r:System.Drawing.dll /r:System.Windows.Forms.dll /target:winexe Controls2.cs
      
      
    • If you don't have admin rights on the computer and you had not changed the path in the Environment Variables, type the following:
      C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\csc /r:System.dll /r:System.Drawing.dll /r:System.Windows.Forms.dll /target:winexe Controls2.cs
      
      
  9. Press Enter
  10. To execute, type Controls2 and press Enter
     
    The location of a Control
  11. To close the form, click its system Close button

The Size of a Control

 

Introduction

The distance from the left border to the right border of a control is referred to as its width property. This can be illustrated as follows:

The location and dimension of a control

The combination of the width and the height of a control is referred to as its size. If you add a control to a form, it assumes a default size.

Specifying the Size of a Control

To support the size of an object, the System.Drawing namespace defines the Size structure that represents a location and dimensions. A Size value must have a starting point (X, Y) just as the Point object. The width is the distance from the left to the right borders of a Size object. The height represents the distance from the top to the bottom borders of a Size value:

Size Representation

To assist you with sizes, the Size structure provides the following constructor:

public Size(int width, int height);

Using this constructor, to specify the size of a control, assign a Size value to its Size property. Here is an example:

using System;
using System.Drawing;
using System.Windows.Forms;

public class Exercise : Form
{
    private Button btnSubmit;

    private void InitializeComponent()
    {
        btnSubmit = new Button();
        btnSubmit.Text = "Submit";
        btnSubmit.Location = new Point(100, 40);
        btnSubmit.Size = new Size(80, 32);

        Controls.Add(btnSubmit);
    }

    public Exercise()
    {
        InitializeComponent();
    }
}

public class Program
{
    static int Main()
    {
        Application.Run(new Exercise());
        return 0;
    }
}

You can also define a Size object using a Point value. To support this, the Size structure is equipped with the following constructor:

public Size(Point pt);

After declaring a variable with this constructor, you can access its Width and Height properties to complete the definition of the Size object. If you already have the size of an object, you may only want to specify the dimensions of the variable. 

Besides the Size, the System.Drawing namespace also provides the SizeF structure. It uses the same properties as Size except that its members float types.

To retrieve the dimensions of a control, you can get its Size property and assign it to a Size object.

ApplicationTopic Applied: Sizing a Control

  1. In Notepad, change the file as follows:
    using System;
    using System.Drawing;
    using System.Windows.Forms;
    
    public class Controls2 : Form
    {
        private Button btnSubmit;
    
        public Controls2()
        {
    	InitializeComponent();
        }
    
        private void InitializeComponent()
        {
            btnSubmit = new Button();
            btnSubmit.Text = "Submit";
            btnSubmit.Location = new Point(12, 12);
            btnSubmit.Size = new System.Drawing.Size(120, 46);
    
    	Size = new System.Drawing.Size(420, 246);
            Controls.Add(btnSubmit);
        }
    }
    
    public class Program
    {
        static int Main()
        {
            Application.Run(new Controls2());
            return 0;
        }
    }
  2. To save the file, on the main menu, click File -> Save
  3. Return to the Command Prompt
  4. Press the up arrow key to make sure the command that builds the project is selected
  5. Press Enter
  6. To execute, type Controls2 and press Enter
     
    Sizing a Control
  7. To close the form, click its system Close button

The Rectangular Location and Size of a Control

The combination of the location and size of an object is represented as a rectangle: a geometric figure with four sides. To support this figure, the System.Drawing namespace provides the Rectangle and the RectangleF structures. A rectangle can be represented as follows:

To completely represent it, a rectangle is defined by its location and its dimensions. The location is defined by a point on the top-left corner of the rectangle. The distance from the left border of the object that owns the rectangle to the left border of the rectangle is represented by a property called Left. The distance from the top border of the object that owns the rectangle to the top border of the rectangle is represented by a property called Top.

The distance from the left to the right borders of the rectangle is represented by a property called Width. The distance from the left to the right borders of the rectangle is represented by a property called Height. The distance from the left border of the object that owns the rectangle to the right border of the rectangle is represented by a property called Right. The distance from the top border of the object that owns the rectangle to the bottom border of the rectangle is represented by a property called Bottom. Based on this, a rectangle can be illustrated as follows:

Rectangle Representation

 

To create a rectangle, you can use the following constructor to declare a Rectangle variable:

public Rectangle(Point location, Size size);

This constructor requires that you define a Point and a Size in order to use it. If instead you know the integer values of the location and dimensions, you can use the following constructor to declare a Rectangle object:

public Rectangle(int x, int y, int width, int height);

Besides the Rectangle structure, the System.Drawing namespace provides the RectangleF structure that uses the same definition as Rectangle, except that it is defined with float values instead of integers.

The be able to recognize the location and the size of a control, the Control class is equipped with a property named Bounds. This property is of type Rectangle represented by the property. Therefore, at any time, to get the location and the size of a control, you can call its Bounds property, which produces a Rectangle value.

Common Properties of Controls

 

The Content Alignment of a Control

When a control is meant to display text, it has a property named TextAlign. The TextAlign property is of type ContentAlignment, which is an enumeration. The members and result of this enumeration are: TopLeft, TopCenter, TopRight, MiddleLeft, MiddleCenter, MiddleRight, BottomLeft, BottomCenter, and BottomRight.

To programmatically specify the text alignment of a control, access its TextAlign property and assign it the desired member of the ContentAlignment enumeration. Here is an example:

using System;
using System.Drawing;
using System.Windows.Forms;

public class Exercise : Form
{
    private Button btnSubmit;

    private void InitializeComponent()
    {
        btnSubmit = new Button();
        btnSubmit.Text = "Submit";
        btnSubmit.Location = new Point(20, 20);
        btnSubmit.Size = new Size(100, 60);
        btnSubmit.TextAlign = ContentAlignment.BottomRight;

        Controls.Add(btnSubmit);
    }

    public Exercise()
    {
        InitializeComponent();
    }
}

public class Program
{
    static int Main()
    {
        Application.Run(new Exercise());
        return 0;
    }
}

Anchoring a Control

The ability to manage a control or a group of controls' location and size when the user resizes it is done using the Anchor property. The Anchor property is created from the AnchorStyles enumeration. By default, when you add a control to a form, its position is relative to the top left corner of its container. The Anchor property can be used to "glue" one border of a control to its parent using one of the following values:

  • Bottom: The control bottom border will be the same even if the parent is heighten or shrunk
  • Left: The control left border will be the same even if the parent is widened or narrowed
  • None: No anchoring is applied
  • Right: The control right border will be the same even if the parent is widened or narrowed
  • Top: The control top border will be the same even if the parent is heightened or shrunk

In the same way, you can combine AnchorStyles values to "glue" one or more corners of a control to its parent when the parent is resized. To programmatically combine two or more AnchorStyles values, you use the OR bit operator: "|"

Control Docking

When a control is added to a host, in some cases, you will want the control to be attached to a border or to a corner of its parent. This can be done using the Dock property. This property is managed through the DockStyle enumeration. To use this property, click the control and, in the Properties window, click the arrow of the Dock field. You can then select one of the following values:

  • Bottom: The control will be attached to the bottom border of its parent
  • Fill: The control will use the whole client area of its parent
  • Left: The control will be attached to the left border of its parent
  • None: The control will be kept where it was positioned on the parent
  • Right: The control will be attached to the right border of its parent
  • Top: The control will be attached to the top border of its parent

To specify the docking option of a control, access its Dock property and assign the desired member of the DockStyle enumeration. Here is an example:

public class Exercise : Form
{
    private Button btnSubmit;

    private void InitializeComponent()
    {
        btnSubmit = new Button();
        btnSubmit.Text = "Submit";
        btnSubmit.Dock = DockStyle.Right;

        Controls.Add(btnSubmit);
    }
}

Aesthetic Aspects of a Control

 

Background Color

Controls used on Microsoft Windows are painted using a color. The names of colors are defined in an enumeration called Color.

To specify the background color of a control, access its BackColor property and assign it the desired member of the Color enumeration. Here is an example:

public class Exercise : Form
{
    private Button btnSubmit;
    
    private void InitializeComponent()
    {
        btnSubmit = new Button();
        btnSubmit.Text = "Submit";
        btnSubmit.BackColor = Color.Aquamarine;

        Controls.Add(btnSubmit);
    }
}

Background Image

Instead of a color, you may want to fill the control with a picture. To do this, you can access the control's BackgroundImage property and assign an Image object to it.

Border Style

Some controls display a border when they are drawn and some others don't. This characteristic is controlled by the BorderStyle property, which is based on BorderStyle enumerator. Its members are: Fixed3D, FixedSingle, and None.

To specify the border style of a control, access its BorderStyle property and assign it the desired BorderStyle member. Here is an example:

using System;
using System.Drawing;
using System.Windows.Forms;

public class Exercise : Form
{
    private Panel pnlViewer;

    private void InitializeComponent()
    {
        pnlViewer = new Panel();
        pnlViewer.BorderStyle = BorderStyle.Fixed3D;
        pnlViewer.BackColor = Color.LightBlue;
        Controls.Add(pnlViewer);
    }

    public Exercise()
    {
        InitializeComponent();
    }
}

public class Program
{
    static int Main()
    {
        Application.Run(new Exercise());
        return 0;
    }
}

API Characteristics of Controls

 

Tab Ordering

A user can navigate through controls using the Tab key. When that key has been pressed, the focus moves from one control to the next. By their designs, not all controls can receive focus and not all controls can participate in tab navigation. Even controls that can receive focus must be primarily included in the tab sequence.

Control's Visibility

A control is referred to as visible if it can be visually located on the screen. A user can use a control only if he or she can see it. The visibility of an object is controlled by a property named Visible, which is Boolean.

If you create a control, by default, it is made visible once you add it to its parent's Controls property. To check whether a control is visible at one time, apply a conditional statement (if or while) to its Visible property and check whether its value is true or false.

Control's Availability

The availability of an object is controlled by a Boolean property named Enabled. To find out whether a control is enabled or not, check its Enabled property.

Allowing Drag n' Drop Operations

Drag n' drop is the ability to drag an object or text from one location or object to another. This is a significant operation in computer use. Although Microsoft Windows fully supports drag n' drop operations, because the operating system cannot predict how the operations should be carried, you must write code.

Various controls support drag n' drop operations. While most controls support them, it is not always realistic to implement the operations on every control. This means that you will have to decide when, how, and what controls of your application will need to allow the user do drag what and drop it where.

A control that allows drag n' drop operations is equipped with a Boolean property named AllowDrop. When you set this property to true, it means the user can either drag something from it or drop something on it. After setting this property, you must write code.

 

ApplicationTopic Applied: Ending the Lesson

  1. To close the Command Prompt, get to it and type exit
  2. Press Enter
 

Previous Copyright © 2011 FunuctionX Next