Home > Enterprise >  How to have the same name for VCL and FMX components?
How to have the same name for VCL and FMX components?

Time:09-17

Delphi manages to have TLabel exist in FMX and VCL. So, how do I create two components, both with the same name, except one is for VCL and one is for FMX?

Yes, I know I can use ifdefs and recompile the library every time. But that is not exactly clean code.

CodePudding user response:

Implement your two components in different Unit Scopes, which were created for this exact purpose.

For example, implement TMyComponent for VCL in Vcl.MyUnit.pas, and implement TMyComponent for FMX in FMX.MyUnit.pas.

Then, to use TMyComponent in any other unit, you can either:

  • use {$IFDEF}s to conditionally specify Vcl.MyUnit or FMX.MyUnit in the uses clause.

  • create separate projects for VCL and FMX, where the VCL project specifies Vcl in its Unit Scope Names compiler setting, and the FMX project specifies FMX, and then you can use just MyUnit in the uses clause.

Also see Adding Unit Scope Names for Your Own Components.

For reference, VCL's TLabel is in the Vcl.StdCtrls unit scope, and FMX's TLabel is in the FMX.StdCtrls unit scope.

  • Related