Home > OS >  Capture selected element in a TForm in design time
Capture selected element in a TForm in design time

Time:09-09

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.

CodePudding user response:

You can write a class that implements the IDesignNotification interface, and then register an instance of that class with the IDE using the RegisterDesignNotification() function. The IDesignNotification.SelectionChanged() method is what you are looking for.

Called when the selected components on a form designer change.

CodePudding user response:

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;
  • Related