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?
Create a design-time package for your component (if you do not already have one). In that package, create a class that implements the
ISelectionEditorinterface (the easiest way is to derive from theTSelectionEditorclass), overriding its virtualRequiresUnits()method to report any additional units you want to appear in theusesclause of any Form/Frame/DataModule that uses your component. Then, have your package’sRegister()function register that class by callingRegisterSelectionEditor()(in addition toRegisterComponents()).Indy 10 does exactly this for its
TIdTCPServer,TIdCmdTCPClient, andTIdUDPServercomponents, to make sure theIdContextandIdSocketHandleunits get added tousesclauses. Look at theIdCoreSelectionEditors.pasandIdRegisterCore.pasunits to see how Indy implements this.Update: the
IdCoreSelectionEditors.pasandIdProtocolsSelectionEditors.pasunits were removed from Indy 10 in March 2014. All of the per-component SelectionEditor classes were replaced with a new single class implemented in theIdRegisterCoreunit 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 theusesclause.