Home

Control Creation

 

Introduction

A Windows control is a graphical object that allows the user to interact with the computer. The controls are as varied as the needs and goals are. Because there are so many controls for various purposes, their insertion to an application and their configuration are left to the application programmer. The Toolbox is the accessory that provides most of the controls used in an application.

By default, the Toolbox is positioned on the left side of the IDE. To change that position, you can drag its title bar away and dock it to another side of the ID. The Toolbox also uses a default width to show the items on it. If the width is too small or too large for you, you can change it. To do this, position the mouse to its right border and drag left or right.

 

The Sections of the Toolbox

When you start a Windows Forms Application, it provides various controls on the Toolbox so you can choose which ones are appropriate for your particular application. Controls can be set by categories based on their function or role. A container is a control whose main purpose is to host other controls. To design it, you pick up objects from the Toolbox and drop them where desired. The Toolbox organizes its items in categories and each category is represented by a button:

Toolbox

If the available list of categories is not enough, you can add a new section of your choice. By default, Visual Studio .NET hides some categories because they are judged hardly used. To display these additional sections, you can right-click anywhere in the Toolbox and click Show All Tabs:

Toolbox

If you still want an additional tab not included in the list, you can add one (or more). To do that, right-click anywhere in the Toolbox and click Add Tab. You would be prompted to provide a name. After typing the new name, press Enter.

 

The Layout of a Category

To use an object of a particular category, you can first click its button. After selecting a category, it displays its items. In each category, a particular button called Pointer is selected by default. This simply indicates that no item in the group is currently selected.

By default, the items in each category are organized as horizontal wide buttons:

Toolbox

Alternatively, you can list the items of a category as buttons of a list view. To do that, you can right-click anywhere in the category and click List View to remove its check box:

Toolbox

 

Arrangement of Items in the Toolbox

When Microsoft Visual Studio .NET is installed, it adds the buttons in a somewhat random order:

Toolbox

In the beginning, this can make it difficult to find a particular control when you need it. If you find it more convenient, you can arrange the list of controls in any order of your choice. You have two main options. To change the position of an item in the list, right-click it and click either Move Up or Move Down. Alternatively, you can arrange the items in alphabetic order. To do that, right-click anywhere in the Windows Forms section and click Sort Items Alphabetically:

Toolbox

Once you have rearranged items alphabetically, the Toolbox forgets the previous arrangement and you cannot restore it. Alternatively, you can right-click the button of a control and click either Move Up or Move Down.

 

Visual Control Addition

To add a control to your application, you can select it from the Toolbox and click the desired area on the form. Once added, the control is positioned where your mouse landed. In the same way, you can add other controls as you judge them necessary for your application. Here is an example of a few controls added to a form:

Form

Alternatively, to add a control, you can also double-click it from the Toolbox and it would be added to the top-left section of the form.

If you want to add a certain control many times, before selecting it on the Toolbox, press and hold Ctrl. Then click it in the Toolbox. This permanently selects the control. Every time you click the form, the control would be added. Once you have added the desired number of this control, on the Toolbox, click the Pointer button to dismiss the control.

 

The Toolbox and Additional Controls

When Microsoft Visual Studio .NET is set up, it installs in the Toolbox the most regularly used controls. If you are working in an environment that creates only a particular group of applications and there are controls you hardly use, if you want, you can remove them from the list. To remove a control, right-click it and click Remove.

Besides the objects in the Windows Forms section, other controls are left out but are still available. Some of the left out controls were created with the .NET Framework but are not installed by default because they are judged hardly used. To add one or more of these left out controls, right-click anywhere in the Toolbox and click Add/Remove Items... In the Customize Toolbox dialog box, click the .NET Framework Components tab, then click the check box of the desired control and click OK:

Customize Toolbox

In addition to custom .NET controls, some other objects called ActiveX controls were used in previous versions of Visual Basic or Visual Studio .NET and are available. To take care of compatibility issues, most previous ActiveX controls were reconfigured and adapted to be used in a .NET application. To add some of these left out controls, right-click anywhere in the Toolbox and click Add/Remove Items... In the Customize Toolbox dialog box, click the COM Components tab, select the desired control and click OK

 

The COM Components property page of the Customize Toolbox

Dynamic Control Creation

 

Introduction

The objects used in a Windows application are defined in various assemblies. To add one of these controls to your application, you must first know the name of its class. With this information, you can declare a variable of its class. For example, a command button is an object of type Button that is based on the Button class. The Button class is defined in the System::Windows::Forms namespace of the System.Windows.Forms.dll assembly. Based on this, to create a button, you can declare a pointer to Button.

If you declare and completely create a control in a particular method, the control would be available only in that method and cannot be accessed outside. To make such a control available to other controls or methods of the same class, you should declare it outside of a method. Here is an example:

#pragma once
#using <mscorlib.dll>

using namespace System;
using namespace System::Windows::Forms;

__gc class CFundamentals : public Form
{
	Button *btnSubmit;

public:
	CFundamentals(void);
};

After declaring the variable, you can use the new operator to allocate memory for it:

CFundamentals::CFundamentals(void)
{
	btnSubmit = new Button();
}

This is also referred to as dynamically creating a control. After declaring the variable and allocating memory for it, the control is available but doesn't have a host, which makes it invisible. You can then call the Controls property of its parent to add the new control to its collection. The parent would then be responsible for hosting or carrying the new control. Here is an example:

CFundamentals::CFundamentals(void)
{
	btnSubmit = new Button();
	
	this->Controls->Add(btnSubmit);
} 

Because there can be many controls used in a program, instead of using the constructor to initialize them, Visual Studio .NET standards recommends that you create a method called InitializeComponent to initialize the various objects used in your application. Then simply call that method from the constructor of your form.

 

Control Addition by Class Derivation

If you are using a .NET Framework control, you must know the name of the class on which the control is based (and each control is based on a particular class). If you have examined the types of classes available but none implements the behavior you need, you can first locate one that is close to the behavior you are looking for, then use it as a base to derive a new class.

To derive a class from an existing control, you can use your knowledge of C++ inheritance to derive a new class. Here is an example:

public __gc class Numeric : public System::Windows::Forms::TextBox
{
};

If you want to perform some early initialization to customize your new control, you can declare a constructor. Here is an example:

public __gc class Numeric : public System::Windows::Forms::TextBox
{
public:
	Numeric() { }
};

Besides the constructor, in your class, you can add the properties, methods, and/or events as you see fit. You can also use it to globally set a value for a property of the parent class. Once the control is ready, you can dynamically use it like any other control. Here is an example:

Header File
#pragma once
#using <mscorlib.dll>

using namespace System;
using namespace System::Windows::Forms;

public __gc class Numeric : public System::Windows::Forms::TextBox
{
public:
	Numeric() { }
};

__gc class CFundamentals : public Form
{
private:
	Numeric *txtBox;
public:
	CFundamentals(void);
};
Source File
#include <windows.h>
#include ".\fundamentals.h"

CFundamentals::CFundamentals(void)
{
	txtBox = new Numeric();
	this->Controls->Add(txtBox);
}

int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPTSTR    lpCmdLine,
                     int       nCmdShow)
{
	// Instantiate an Exercise object
	CFundamentals *frmMain;

	// Allocate memory for the object, using the new operator
	frmMain = new CFundamentals();
	// Call the Run() static method of the Application
	// and pass it the instance of the class to display
	Application::Run(frmMain);

	// Everything went alright... We hope
	return 0;
}

 

 

Previous Copyright © 2004-2015 FunctionX, Inc. Next