Home

Characteristics of Windows Controls

 

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 defined 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.

 

Practical LearningPractical Learning: Introducing Characteristics of Controls

  1. Start Notepad and, in the empty file, type the following
     
    using System;
    using System.Windows.Forms;
    
    namespace WindowsFundamentals
    {
      class Exercise : System.Windows.Forms.Form
      {
        public Exercise()
        {
    	InitializeComponent();
        }
    
        private void InitializeComponent()
        {
        }
    
        static int Main()
        {
    	Application.Run(new Exercise());
    
    	return 0;
        }
      }
    }
  2. Save the file in a new folder named Fundamentals3
  3. Save the file itself as Exercise.cs
  4. Open the Command Prompt and change to the Fundamentals3 folder that contains the above file
  5. To compile the application, execute the following command
     
    csc /r:System.Windows.Forms.dll Exercise.cs
  6. To execute the application, type Exercise and press Enter
  7. Close the form and return to Notepad

A Control's Handle

Up to 2002, Microsoft Windows programmers relied on a library called Win32 to create applications for the operating system. The Win32 library was the only resource of classes (in fact structures), functions, etc, that gave functional instructions to the operating system. The other languages such as Pascal, Visual Basic, etc, used directly or indirectly these resources in their own "dialect" to communicate with Microsoft Windows. The Win32 library is still around (it can never disappear unless the operating system changes completely) and serves as the platform for Microsoft Windows programming. To harmonize programming for this platform, Microsoft created the .NET Framework as a substitute library. This is the library used in Microsoft Visual Studio .NET programming environment. Most of the functionality of Win32 has been redefined in the .NET Framework. The operations were implemented in various libraries or assemblies. Some other operations, such as those related to the Registry, were kept in the Microsoft.Win32 namespace.

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. The new .NET Framework also uses handles to internally represent controls but defines a handle as a pointer to an integer. Based on this, every control has a handle. 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 Text of a Control

Some controls, as we will see, are text-based, meaning they are meant to display or sometimes request text from the user. For such controls, this text is referred to as caption while it is simply called text for some other controls. This property is not available for all controls.

If a control displays text, it then has a Text property. To specify the text of a control, assign a string to its Text property. Alternatively, if you want, you can format it to display a more complex string such as the current date or time, the current username, etc.

Practical LearningPractical Learning: Setting Controls Text

  1. To experiment with the Text property of a control, change the file as follows:
     
    using System;
    using System.Windows.Forms;
    
    namespace WindowsFundamentals
    {
      class Exercise : System.Windows.Forms.Form
      {
        public Exercise()
        {
    	InitializeComponent();
        }
    
        private void InitializeComponent()
        {
    	// Set this form's caption
    	this.Text = "Windows Fundamentals";
        }
    
        static int Main()
        {
    	Application.Run(new Exercise());
    
    	return 0;
        }
      }
    }
  2. Save the file
  3. Return to the Command Prompt and compile it the same way you did above
     
    csc /r:System.Windows.Forms.dll Exercise.cs
  4. Execute it the same way you did above
     
    A form with a caption
  5. Close the form and return to Notepad

Control's Names

To create a control, the primary piece of information you must provide is its name. This allows you and the compiler to know what control you are referring to when the program is running. You specify the name of a control when declaring it, the same way you must provide a name to every variable you declare in your program. You can also change the name of a control programmatically but you should avoid doing that unless you have a good reason to. To programmatically change the name of a control, access its Name property and assign a string, using the C# rules of variable names. Here is an example:

this.checkBox1.Name = "chkPepperoni";

To retrieve the name of a control, access its Name property.

 

Practical LearningPractical Learning: Naming Controls

  1. To experiment with giving a name to a control, change the file as follows:
     
    using System;
    using System.Windows.Forms;
    
    namespace WindowsFundamentals
    {
      class Exercise : System.Windows.Forms.Form
      {
        // Declare a control and give it a name
        private Button btnSend;
    
        public Exercise()
        {
    	InitializeComponent();
        }
    
        private void InitializeComponent()
        {
    	// The btnSend button
    	btnSend = new Button();
    	btnSend.Name = "btnSend";
    
    	// The main form
    	this.Text = "Windows Fundamentals";
    	this.Controls.Add(btnSend);
        }
    
        static int Main()
        {
    	Application.Run(new Exercise());
    
    	return 0;
        }
      }
    }
  2. Save the file
  3. Return to the Command Prompt and compile it the same way you did above
  4. Execute it the same way you did above
     
  5. Close the form and return to Notepad
 

Controls Location

 

Introduction

The controls added to a parent window are confined to the area of the body offered by that parent window. When you create a new control and add it to the parent, the control is positioned in the top-left corner of its parent as seen above. The position, also called location, of the control in the body of the parent uses a Cartesian coordinate system whose origin is located on the top-left corner of the parent window. If the parent is a form, the origin is located just under the title bar to the left. The horizontal measurements move from the origin to the right. The vertical measurements move from the origin to the bottom:

The origin of the coordinate system and its axes

The distance from the controlís left border to the parentís left border is represented with the Left property. The distance from the controlís top border to the parentís top border is referred to as the Top property. The Left and Top values are known as the controlís location. This can be illustrated as follows:

The location of a control inside a parent window

Instead of using the default location, to specify the desired location, you have two options. You can assign an integer to the Top property of the control and/or another integer to the Left property of the control. Alternatively, you can assign a Point value to the control's Location property.

Practical LearningPractical Learning: Setting Controls Text

  1. To set a control's location, change the file as follows:
     
    using System;
    using System.Windows.Forms;
    
    namespace WindowsFundamentals
    {
      class Exercise : System.Windows.Forms.Form
      {
        // Declare a control and give it a name
        private Button btnSend;
    
        public Exercise()
        {
    	InitializeComponent();
        }
    
        private void InitializeComponent()
        {
    	// The btnSend button
    	btnSend = new Button();
    	btnSend.Name = "btnSend";
    	// The location of the control
    	btnSend.Location = new System.Drawing.Point(20, 124);
    
    	// The main form
    	this.Text = "Windows Fundamentals";
    	this.Controls.Add(btnSend);
        }
    
        static int Main()
        {
    	Application.Run(new Exercise());
    
    	return 0;
        }
      }
    }
  2. Save the file
  3. Return to the Command Prompt and compile it the same way you did previously
     
    csc /r:System.Windows.Forms.dll Exercise.cs
  4. Execute it the same way you did above
  5. Close the form and return to Notepad

The Client Area

As mentioned already, a control is confined to the area made available by its parent. This section is called the client area. For a form, the client area is the body of the form without the title bar, its borders and other sections we have not mentioned yet such as the menu, scroll bars, etc:

 

Besides the form, every control also has a client area. The role of the client area is to specify the bounding section where the control can be accessed by other controls positioned on it. Based on this, a control can be visible only within the client area of its parent.

The client area is programmatically represented by a property called ClientRectangle.

Controls Dimensions

 

Introduction

The distance from the left border to the right border of a control is referred to as its Width property. In the same way, the distance from the top to the bottom borders of a control is its Height property. This can be illustrated as follows:

The location and dimension of a control

When you add a control to a form, the control assumes some default dimensions in the body of the parent. To set or change the size of a control, you can assign a Size value to its Size property.

The Bounding Rectangle of a Control

When a control has been added to a container, it occupies a Rectangle represented by the Bounds property. The syntax of this property is:

public Rectangle Bounds {get; set}; 

At any time, to get the location and the dimensions of a control, you can call its Bounds property, which produces a Rectangle value.

 

Control Anchoring

If you position a (visual) control on a form, if the control is positioned on the top left section of the form, when the user resizes the form, the controlís position would appear static, it would not move. This could be a concern if the control is positioned on the right, the bottom or the lower right sections of the form. When the user resizes the form, the controlís position would not be updated. Sometimes you will want the control to have the same location and/or distance with regard to the bottom, the right, and/or the lower right corners of the form.

The ability to manage a control or a group of controls' location and size when the use resizes it is done using the Anchor property. The Anchor property is created from the AnchorStyles enumerator. By default, when you add a control to a form, it is ďgluedĒ to the top left corner of its container. You can also set the controlís position with regards to its containerís right and bottom borders. 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

btnSend.Anchor = AnchorStyles.Bottom;

Left: The control left border will be the same even if the parent is widened or narrowed

btnSend.Anchor = AnchorStyles.Left;


None: No anchoring is applied

btnSend.Anchor = AnchorStyles.None;

Right: The control right border will be the same even if the parent is widened or narrowed

btnSend.Anchor = AnchorStyles.Right;


Top: The control top border will be the same even if the parent is heightened or shrunk

btnSend.Anchor = AnchorStyles.Top;


In the same way, you can combine Anchor values to "glue" one or more corners of a control to its parent when the parent is resized.

Control Docking

When a control is added to a form, 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. The Dock property is managed through the DockStyle enumerator. To use this property, assign the qualified name of a member of the DockStyle enumerator to the control's Dock property. The members of this enumerator are:

Bottom: The control will be attached to the bottom border of its parent:

btnSend.Dock = Dock.Bottom;

Fill: The control will use the whole client area of its parent:

btnSend.Dock = Dock.Fill;

Left: The control will be attached to the left border of its parent:

btnSend.Dock = Dock.Left;

None: The control will be kept where it was positioned on the parent:

btnSend.Dock = Dock.None;

Right: The control will be attached to the right border of its parent:

btnSend.Dock = Dock.Right;

Top: The control will be attached to the top border of its parent:

btnSend.Dock = Dock.Top;

 

Aesthetic Aspects of Controls

 

Background Color

Controls used on Microsoft Windows are painted using a color known as Control. If you don't like the default color that paints the background of a control, you can assign a color to the control's BackColor property.

Background Image

Instead of a color, you may want to fill the control with a picture. To do this, you can assign a picture file to the control's BackgroundImage property.

Border Style

Some controls display a border when they are drawn and some others don't. Consider the following:

Some of these controls allow you to specify a type of border you want to show surrounding the control. This characteristic is controlled by the BorderStyle property, which is based on BorderStyle enumerator. Its members are:

Value Example
Fixed3D
FixedSingle
None
 

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.

The participation of a control to the tab sequence is controlled by its TabStop property. By default, every visual control that can receive focus is already configured to have this property set to true. If you want to remove a control from this sequence, set its TabStop value to False.

If a control has the TabStop property set to true, to arrange the navigation order of controls,  change the value of its TabIndex field. The value must be a positive short integer.

Flat Styles

Microsoft Windows XP added a new feature to the way some controls display or behave when the mouse passes over them, making the controls significantly attractive and enhanced. This feature is neither automatically applied on controls nor available to all controls. Therefore, before using it, you must first find out if the control you want to use it on support it. A control that supports this feature should have the FlatStyle property. This property provides the following values:

  • Flat: The control appears flat. When the mouse is over it, it becomes highlighted

  • Popup: The control appears flat. When the mouse is over it, the borders of the control are raised

  • Standard: The control appears and behave like all regular controls you have seen

  • System: The appearance of the control depends on the operating system using it

If the application will be run on Windows XP, you can use the System value of the FlatStyle property. To make this style work, call the Application.EnableVisualStyles() method in the Main() method as follows:

static void Main() 
{
	Application.EnableVisualStyles();
	Application.Run(new Form1());
}

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. As a programmer, you have the role of deciding whether a control must be seen or not and when. The visibility of an object is controlled by the its Boolean Visible property.

At design time, when you add a control to a form, it is visible by default. This is because its Visible property is set to true. In the same way, by default, it is made visible once you add it to its parent's Controls property.

If you don't want a control to primarily appear when the form comes up, you can either set its Visible property to false or set its parentís visible property to false. Keep in mind that when a parent gets hidden, it also hides its children. On the other hand, a parent can be made visible but hide one or some of its children.

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. Here is example:

this.textBox1.Visible = false;

Control's Availability

For the user to use a control, it must allow operations on it. For example, if a control is supposed to receive text, the user can enter characters in it only if this is made possible. To make a control available to the user, the object must be enabled. The availability of an object is controlled by the Boolean Enabled property.

By default, after adding a control to a form, it is enabled and its Enabled property is set to true. An enabled control displays its text or other characteristics in their normal settings. If you want to disable a control, set its Enabled property to false. In the following picture, a text box and a button are disabled:

Enabled

To find out whether a control is enabled or not, check its Enabled property state.

 

 

Previous Copyright © 2004-2005 FunctionX, Inc. Next