OTA - Populating the Form with Components

Adding Components to a newly created Form is easy. Simply use the IOTAModuleCreator.FormCreated method

procedure IOTAModuleCreator.FormCreated(const FormEditor: IOTAFormEditor);

 The method is called after the Module has been created and has had an Editor assigned to it. We simply use the Editor to add components to the Form. These Components must be known to the IDE. Typically if the Components can be used at design time then they can be created dynamically in a Wizard. When developing a Component it may be necessary to use the RegisterClass function to register the Component class with the IDE so it can create an instance of the Component through the streaming system.

procedure RegisterClass(AClass: TPersistentClass);

  In the Project Creator demo 4 TEdit's are created in the Form. Note you must make sure the type of Component you are creating is applicable to the type of Form it is created on. For instance a TEdit is not a valid Component for a DataModule.

procedure TFormCreatorModule.FormCreated(const FormEditor: IOTAFormEditor);
begin
  inherited;
  FormEditor.CreateComponent(nil, 'TEdit', 10, 10, 180, 26);
  FormEditor.CreateComponent(nil, 'TEdit', 10, 40, 180, 26);
  FormEditor.CreateComponent(nil, 'TEdit', 10, 70, 180, 26);
  FormEditor.CreateComponent(nil, 'TEdit', 10, 100, 180, 26);
end;

Another way of accomplishing the same task is to retrieve the IOTAFormEditor in theWizard Execute method. A bit more work is involved since the Editor interface must be retrieved from the Module.

procedure TProjectCreatorWizard.Execute;
var
  ProjectModule,
  FormModule,
  DataModule,
  FrameModule: IOTAModule;
  FormEditor: IOTAFormEditor;
begin
  // First create the Project
  ProjectModule := (BorlandIDEServices as IOTAModuleServices).CreateModule(TProjectCreatorModule.Create);
  FormModule := (BorlandIDEServices as IOTAModuleServices).CreateModule(TFormCreatorModule.Create);
  // Now create a DataModue for the Project since the code added to the Project expects it.
  //....

  // Another way to get the FormEditor to create Components. The TEdits are
  // created in the FormCreated method in TFormCreatorModule.
  FormEditor := FormEditorFromForm(FormModule);
  if Assigned(FormEditor) then
  begin
  FormEditor.CreateComponent(nil, 'TButton', 10, 130, 57, 21);
  FormEditor.CreateComponent(nil, 'TButton', 100, 130, 57, 21);
  end
end;

  Where FormEditorFromForm is a function in the OTAUtilities.pas helper file supplied with the demo and is defined as

function FormEditorFromForm(Module: IOTAModule): IOTAFormEditor;
var
  i: Integer;
  Done: Boolean;
begin
  Result := nil;
  i := 0;
  Done := False;
  while (i < Module.GetModuleFileCount) and not Done do
  begin
  {$IFDEF COMPILER_6_UP}
  Done := Succeeded( Module.ModuleFileEditors[i].QueryInterface(IOTAFormEditor, Result));
  {$ELSE}
  Done := Succeeded( Module.GetModuleFileEditor(i).QueryInterface(IOTAFormEditor, Result));
  {$ENDIF}
  Inc(i);
  end
end;

  where this method simply attempts to find an IOAFormEditor in the list of Editors defined by the Module.

 

  Back to OpenTools API page.

 


mustangpeak.net

  Last Modified on: