Raffaele Rossi
Raffaele Rossi

Reputation: 3127

Delphi cannot drag and drop custom components

I have written some components for the IDE and they all look like this:

unit xxx;

interface

uses
 System.Classes, System.SysUtils;

type
 TMyClass = class(TComponent)
  //code...
 end;

implementation

end.

In this way I can use them on VCL and FMX; in other words I can make a VCL win program and a FMX Android app that use my components. The code you can't see is the implementation of the class but that's not important.

  1. When I start a VCL project I can drag/drop my components and use them.
  2. When I start an FMX win32 project I can drag/drop my components and use them. If I switch the platform to Android the component is still there and I can compile with success. By the way I cannot drag/drop the component. Why?

In point 2 I mean that under FMX win32 I have the component like this:

enter image description here

If I change platform (say android) I get this

enter image description here

What am I doing wrong? I went in IDE > Tools > Options > Library and for each platform I have added the path in which the IDE can find the .pas with the sources. Any idea?

The weird thing is that under FMX if I want to be able to create an Android app with my components I have to change the platform to win32, drag-drop the component and then change back to Android. The component works very well on android too.

Upvotes: 3

Views: 570

Answers (1)

Alberto Miola
Alberto Miola

Reputation: 4751

You just need to include System.Classes (you already have it from what I can see) and then be usure to use this attribute:

type
 [ComponentPlatformsAttribute(pidWin32 or pidAndroid)]
 TTestComponent = class(TComponent)
  //...  
 end;

In this way you will be able to drag and drop components from the IDE to the view even when you've switched to Android. Basically your code is fine and it works with VCL and FMX but with that attribute you are telling the IDE that the component is compatible with the platforms you are specifying.

ComponentPlatformsAttribute gives component builders more specific control over the exposed components.

To fix the problem you should:

  1. Open the project of your component
  2. Right click the component in the project manager > click Uninstall
  3. Add the attribute above your class definition
  4. Save all, compile, build and Install.

You can find more arguments to pass to the constructor of the attribute, I have found them in the System.Classes pas file:

  pidWin32        = $0001;
  pidWin64        = $0002;
  pidOSX32        = $0004;
  pidiOSSimulator = $0008;
  pidAndroid      = $0010;
  pidLinux32      = $0020;
  pidiOSDevice32  = $0040;
  pidiOSDevice    = pidiOSDevice32;// deprecated 'Use pidiOSDevice32';
  pidLinux64      = $0080;

  pidWinNX32      = $0100;
  pidWinIoT32     = $0200; // Embedded IoT (Internet of Things) Windows w/ Intel Galileo
  pidiOSDevice64  = $0400;
  pidWinARM       = $0800;
  pidOSX64        = $1000;
  pidOSXNX64      = pidOSX64 deprecated 'Use pidOSX64';
  pidLinux32Arm   = $2000;
  pidLinux64Arm   = $4000;
  pidAndroid64    = $8000;

There is an answer here that can be helpful if you want to include every single platform, you can pass 0 to the constructor of the attribute like [ComponentPlatformsAttribute(0)]. It seems that it works but I wouldn't use it, I prefer declaring the platforms one by one even if it can be tedious!

Upvotes: 4

Related Questions