VCL Controls: The Data Module



Like the form and the frame controls, a data module is a control container that is created independent of a form. Like the form and the frame, once created, a data module has its own unit that can be used to support its child controls. Unlike all other containers we will review here, a data module can receive or host only non-visual controls; that is, controls that the user does not see.

Data Module Creation

A data module is particularly easy to create and manage since there is no true graphical design to perform on its children. This simply means that the controls placed on a data module do not have a location or dimensions. They can be placed anywhere in its window: the user will see neither the data module nor its children.

To create a data module, on the main menu, you can click File -> New -> Data Module. Alternatively, you can open the New Items dialog box from where you would select Data Module. Any of these two actions would place a rectangular window with a white background on your screen.

To use a data module, you can position only non-visual controls to it. In fact, when the data module window is selected or has focus and you click a tab of the Component Palette, only non-visual controls would be available. For example, here is the Standard property page of the Component Palette when a data module is the top window:

To use a control, click it from the Component Palette and place it on the data module. Here is an example with various controls:

To access a control placed on a data module, do so indirectly through the data module as a control. For example, the following code accesses the ColorDialog1 object from a data module named DataModule2 when the user double-clicks the form. If the user clicks OK after changing the color of the dialog box, the new color would be used to paint the form:


#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
#include "Unit2.h"
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
__fastcall TForm1::TForm1(TComponent* Owner)
	: TForm(Owner)
void __fastcall TForm1::FormDblClick(TObject *Sender)
	if( DataModule2->ColorDialog1->Execute() )
		Color = DataModule2->ColorDialog1->Color;
Home Copyright 2005-2007 FunctionX, Inc.