TDataModule has changed in XE2
Follow these steps:
- File | New | VCL Forms Application
- File | New | Other | Delphi Files | DataModule
You should now have a win32 VCL project with a datamodule. Select the data module and drop a TImageList component on it.
I’ll wait while you try it…
Looking at the Tool Palette with the data module selected displays a list of available component categories – so many are now missing (such as TImageList, TTimer and TActionList).
As you can see that is a pretty selective list of component categories. The reason there are so few components available to be placed on the data module is because a data module is a multiple framework container (it is declared in System.Classes). This means that only components compatible with the VCL and FireMonkey framework are visible by default. Luckily, there is a way to allow previously filtered components a place on the tool palette.
A new property
Looking at the list of properties for the data module you will see there is a new one.
This new property is called ClassGroup and allows you to define what group of classes are allowed on the data module.
Group of Classes (wth)
TComponent descendants (that are only permitted on a data module) can influence which framework they are aligned to. They can either allow use in any framework by grouping with the System.Classes.TPersistent type or can move up in the inheritance chain and choose to be aligned specifically with the VCL or FireMonkey frameworks.
You’ll notice that with all of the unit scoping added to XE2, both frameworks share quite a lot of code. The shared framework code unit names do not start with FMX or Vcl. So the System.Classes unit can be used in either a VCL or FireMonkey application without issue. This is why only dual framework compatible components are displayed when the data module is first created.
Example of how to associate a TComponent descendant to the VCL framework.
To associate with the FireMonkey framework, replace Vcl.Controls.TControl with FMX.Controls.TControl.
Back to the ClassGroup property
I’d point you to the documentation on the wiki about this “property” except it isn’t documented (http://docwiki.embarcadero.com/VCL/XE2/en/Classes.TDataModule_Properties).
I’ve put the word “property” in quotes because it really isn’t a property as you’d normally expect. The value isn’t persisted to the DFM/FMX file it’s value is stored in the actual pascal unit that accompanies the data module.
Dropping down the property displays four choices:
||DataModule only allows cross framework enabled components to be placed on it.
||DataModule allows components added to the Vcl.Controls.TControl class group to be added to it.
||DataModule allows components added to the FMX.Controls.TControl class group to be added to it.
||This is for iOS controls.
Change the ClassGroup property value to Vcl.Controls.TControl.
Currently the Tool Palette doesn’t always refresh when the value is changed, so you may need to either close and reopen the file or change to another open file in the IDE and back to the data module to see the Tool Palette update.
You can now grab a TImageList and place it on the data module.
Storing the ClassGroup value
If you hit F12 (and it works) to view the source code you will see a line added to the source file under the implementation section of the unit.
Don’t delete this line, or you’ll lose the setting of the ClassGroup “property”. This particular property is not stored in the DFM or FMX file. If you do happen to remove the line, the property value is cleared.
Changing the ClassGroup value
If you now try to change the value of ClassGroup to FMX.Controls.TControl you will be prompted with the following error. You need to fix these errors before you can change the ClassGroup value successfully.
When loading previously created data modules, you won’t see your non-compliant components disappear or get deleted. They will still happily display and you can still run your application without needing to change the ClassGroup property. You will not be able to add one of these non-compliant components to the data module until you change the ClassGroup value though.