Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle double click from childs in compound component?

I have created a new compound component based on a TCustomPanel. On it I have two labels and one image, covering all the surface, see this layout (the lower part is less important):

Layout of the control

My question is how to export the double click functionality of any of these controls? Is it a possibility to use the double click (event) of the new control to manage those of the child controls on it?

like image 640
Laurent Saragosti Avatar asked Nov 18 '16 10:11

Laurent Saragosti


2 Answers

I think you should do it by using the following approach:

  1. Add an OnDblClick event to the compound component.
  2. Add a method called FOnInternalDblClick (The name is not important), compatible with TNotifyEvent to your compound component.
  3. Inside FOnInternalDblClick, execute the compound component's OnDblClick.
  4. In the compound component's constructor, assign FOnInternalDblClick to the OnDblClick event of every component for which you want to manage the event.

Example code:

TMyCompoundComponent = class(TCustomPanel)
protected
  FOnDblClick : TNotifyEvent;
  procedure FOnInternalDblClick(ASender : TObject);
public
  constructor Create(AOwner : TComponent); override;
published
  property OnDblClick : TNotifyEvent read FOnDblClick write FOnDblClick;
end;

constructor TMyCompoundComponent.Create(AOwner : TComponent);
begin
  inherited;
  //Lab1.OnDblClick := FOnInternalDblClick;
  //Lab2.OnDblClick := FOnInternalDblClick;
  //...
end;

procedure TMyCompoundComponent.FOnInternalDblClick(ASender : TObject);
begin
  if(Assigned(FOnDblClick))
  then FOnDblClick(ASender);
end;

Note:

In the compound component's OnDblClick event handler, the ASender parameter will be the internal component (Lab1, Lab2, Lab3...). If you prefer to receive the compound component itself as ASender parameter, you could change the FOnInternalDblClick method by passing Self instead of ASender:

procedure TMyCompoundComponent.FOnInternalDblClick(ASender : TObject);
begin
  if(Assigned(FOnDblClick))
  then FOnDblClick(Self);
end;
like image 166
Fabrizio Avatar answered Nov 15 '22 08:11

Fabrizio


Both your requests are possible. It depends on what you want to do. If you want to be able to write your code in the program separately for each subitem you need to create three extra published properties of your component and map them to your corresponding subcomponent properties. Like this (shown for one subcomponent only - repeat for the other 2):

type
  TMyPanelForm1 = class( TPanel )
  private
    fLabel1, fLabel2 : TLabel;
    fImage : TImage;
    procedure SetLabel1DblClick(const Value: TNotifyEvent);
    function GetLabel1DblClick: TNotifyEvent;
  public
    constructor Create(AOwner: TComponent); override;
  published
    property OnLabel1DblClick : TNotifyEvent
             read GetLabel1DblClick write SetLabel1DblClick;
  end;
...
function TMyPanelForm1.GetLabel1DblClick: TNotifyEvent;
begin
  Result := fLabel1.OnDblClick;
end;

procedure TMyPanelForm1.SetLabel1DblClick(const Value: TNotifyEvent);
begin
  fLabel1.OnDblClick := Value;

end;

On the other hand, if you want the control to act like a unified control, where all three subcontrols 'inherit' the main component double click, you just ripple down assignments like this:

  TMyPanelForm2 = class( TPanel )
  private
    fLabel1, fLabel2 : TLabel;
    fImage : TImage;
    function GetOnDblClick: TNotifyEvent;
    procedure SetOnDblClick(const Value: TNotifyEvent);
  public
    constructor Create(AOwner: TComponent); override;
  published
    property OnDblClick : TNotifyEvent
             read GetOnDblClick write SetOnDblClick;
  end;
...
function TMyPanelForm2.GetOnDblClick: TNotifyEvent;
begin
  Result := inherited OnDblClick;
end;

procedure TMyPanelForm2.SetOnDblClick(const Value: TNotifyEvent);
begin
  inherited OnDblClick := Value;
  fLabel1.OnDblClick := Value;
  fLabel2.OnDblClick := Value;
  fImage.OnDblClick := Value;
end;

Other solutions are possible too.

like image 34
Dsm Avatar answered Nov 15 '22 10:11

Dsm