Search code examples
delphidelphi-7

Capture selected element in a TForm in design time


I'm making a productivity tool that presents information about a form. One of its components is a TreeView that represents the hierarchy of objects inside the form.

Right now I'm able to change the selected item in the form as I change the selected node in the tree Selected item

I'm simply using this code when the tree view selection changes:

procedure SeleccionarComponente(const Nombre: string);
var
  FormEditor: IOTAFormEditor;
  Componente: IOTAComponent;
begin
  // Seleccionar el componente en el editor de formularios
  FormEditor := GetCurrentFormEditor;
  Componente := FormEditor.FindComponent(Nombre);
  if Componente <> nil then
    Componente.Select(False);
end;

Is there a way to do it the other way? I want to change the tree view selection whenever a component is clicked on the form.


Solution

  • The solution was quite trivial, in the end.

    My form inherits from TDesignWindow:

    TVentanaVisorComponentes = class(TDesignWindow)
    

    So I can override some methods

    procedure ItemDeleted(const ADesigner: IDesigner; AItem: TPersistent); override;
    procedure ItemsModified(const ADesigner: IDesigner); override;
    procedure ItemInserted(const ADesigner: IDesigner; Item: TPersistent); override;
    procedure SelectionChanged(const ADesigner: IDesigner; const ASelection: IDesignerSelections); override;
    procedure DesignerClosed(const ADesigner: IDesigner; AGoingDormant: Boolean); override;
    

    The method SelectionChanged did the trick

    procedure TVentanaVisorComponentes.SelectionChanged(
      const ADesigner: IDesigner; const ASelection: IDesignerSelections);
    var
      NuevaSeleccion: TNodoArbolComponentes;
    begin
      inherited;
      if (ASelection.Count = 1) and (ASelection.Items[0] is TComponent) then
      begin
        NuevaSeleccion := BuscarNodoPorComponente(ASelection.Items[0] as TComponent);
        if NuevaSeleccion <> nil then
          TreeView1.Selected := NuevaSeleccion;
      end;
    end;