Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I make a custom Delphi component add multiple units to the uses clause?

I'm working on some components in XE2. Is it possible to have more than the component's unit added to the uses clause of the form it's dropped on?

Example: When I choose the TCustomComponent from a package I've built and installed the unit CustomComponent is added to the form's uses clause. I would like to also add the unit GlobalConstants.

Does anyone know if this can be done?

like image 851
DelphiDabber Avatar asked Jan 15 '13 06:01

DelphiDabber


1 Answers

Create a design-time package for your component (if you do not already have one). In that package, create a class that implements the ISelectionEditor interface (the easiest way is to derive from the TSelectionEditor class), overriding its virtual RequiresUnits() method to report any additional units you want to appear in the uses clause of any Form/Frame/DataModule that uses your component. Then, have your package's Register() function register that class by calling RegisterSelectionEditor() (in addition to RegisterComponents()).

Indy 10 does exactly this for its TIdTCPServer, TIdCmdTCPClient, and TIdUDPServer components, to make sure the IdContext and IdSocketHandle units get added to uses clauses. Look at the IdCoreSelectionEditors.pas and IdRegisterCore.pas units to see how Indy implements this.

Update: the IdCoreSelectionEditors.pas and IdProtocolsSelectionEditors.pas units were removed from Indy 10 in March 2014. All of the per-component SelectionEditor classes were replaced with a new single class implemented in the IdRegisterCore unit itself. This new SelectionEditor class is registered for all Indy components, and it looks for all instances of any Indy component that has been placed at design-time, using RTTI of the data types of all parameters and return values for any assigned event handlers to know which units to report for inclusion in the uses clause.

like image 68
Remy Lebeau Avatar answered Nov 11 '22 03:11

Remy Lebeau