How to expose an OnClick event of a compound component lazarus / 31-LCL ---------------------------------------------------------------------- andrew Bubble · 2017-05-06 08:17 Hi All I have made a compound component called TLedPanel (TCustomPanel) consisting of a TPanel, TShape and a TLabel. The component all works except in one area. I have exposed the OnClick event of the TLedPanel by placing this in the published section, If I place the new component on the form and double click then the OnClick procedure is shown in lazarus as normal. The issue is in run time the Oncllick event does not fire, I am assuming as the TLabel on the component is in front the OnClick event of this is actually firing. not the TCustomPanel. My question is how can I expose the TLabel OnClick event at design time. Regards Andrew ---------------------------------------------------------------------- wp · 2017-05-06 08:20 No chance to give you a solid answer without having seen your code. ---------------------------------------------------------------------- andrew Bubble · 2017-05-06 08:30 Hi wp Unfortunately the code for the component is quite long but basically I have placed the new component's OnClick event as below.  published     { Published declarations }     property OnClick; This works as expected in design time but when I run the component and click the event does not fire. Regards Andrew ---------------------------------------------------------------------- wp · 2017-05-06 08:56 You must make sure that the event is called. Just calling a procedure an event does not mean that it is called... Here's an example how I would do it. The OnClick fires when you click on the LED or the label. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i unit Unit1; {$mode objfpc}{$H+} interface uses  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls, ExtCtrls; type  TLedPanel = class(TPanel)  private    FLabel: TLabel;    FLED: TShape;    FOnClick: TNotifyEvent;  protected    procedure ClickHandler(Sender: TObject);  public    constructor Create(AOwner: TComponent); override;    procedure Toggle;  published    property OnClick: TNotifyEvent read FOnClick write FOnClick;  end;   { TForm1 }   TForm1 = class(TForm)    procedure FormCreate(Sender: TObject);  private    FCounter: Integer;    FLedPanel: TLedPanel;    procedure LedPanelClick(Sender: TObject);   public   end; var  Form1: TForm1; implementation {$R *.lfm} { TForm1 } procedure TForm1.FormCreate(Sender: TObject);begin  FLedPanel := TLedPanel.Create(self);  FLedPanel.OnClick := @LedPanelClick;  FLedPanel.Parent := self;end; procedure TForm1.LedPanelClick(Sender: TObject);begin  inc(FCounter);  Caption := 'Click counter ' + IntToStr(FCounter);end; constructor TLedPanel.Create(AOwner: TComponent);begin  inherited;  FLed := TShape.Create(self);  FLed.Shape := stCircle;  FLed.Parent := self;  FLed.Width := 16;  FLed.Height := 16;  FLed.Brush.Color := clRed;  FLed.OnClick := @ClickHandler;   FLabel := TLabel.Create(self);  FLabel.Parent := Self;  FLabel.Left := FLed.Width + 8;  FLabel.Caption := 'OFF';  FLabel.OnClick := @ClickHandler;end; procedure TLedPanel.ClickHandler(Sender: TObject);begin  Toggle;  if Assigned(FOnClick) then FOnClick(self);end; procedure TLedPanel.Toggle;begin  if FLed.Brush.Color = clRed then begin    FLed.Brush.Color := clGreen;    FLabel.Caption := 'ON';  end else begin    FLed.Brush.Color := clRed;    FLabel.Caption := 'OFF';  end;end; end. ---------------------------------------------------------------------- balazsszekely · 2017-05-06 08:57 Hi Andrew, If I understood you correctly you need to redirect all OnClick events to a single one. Please try something like this(untested): Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i unit LadPanelUnit; {$mode objfpc}{$H+} interface uses  Classes, SysUtils, ExtCtrls, StdCtrls, Graphics; type   { TLedPanel }   TLedPanel = class(TPanel)  private    FLabel: TLabel;    FShape: TShape;    FOnLedClick: TNotifyEvent;    procedure DoOnClick(Sender: TObject);  public    constructor Create(TheOwner: TComponent);    destructor Destroy; override;  published    property OnLedClick: TNotifyEvent read FOnLedClick write FOnLedClick;  end;  implementation { TLedPanel } procedure TLedPanel.DoOnClick(Sender: TObject);begin  if Assigned(FOnLedClick) then    FOnLedClick(Self);end; constructor TLedPanel.Create(TheOwner: TComponent);begin  inherited Create(nil);  Self.Width := 300;  Self.Height := 200;  Self.OnClick := @DoOnClick;   FLabel := TLabel.Create(nil);  with FLabel do  begin    Parent := Self;    Left := 50;    Top := 10;    Caption := 'Test';    OnClick := @DoOnClick;  end;  FShape := TShape.Create(nil);  with FShape do  begin    Parent := Self;    Left := 120;    Top := 55;    Pen.Color := clBlack;    Brush.Color := clRed;    OnClick := @DoOnClick;  end;end; destructor TLedPanel.Destroy;begin  FLabel.Free;  FShape.Free;end; end. ---------------------------------------------------------------------- andrew Bubble · 2017-05-06 09:12 Hi All Thanks for your help, it was a while ago when I did component development and that was with Delphi 7, I though the issue was the event handler. I was having issues with the assigning the event handler, I was using Self.OnClick := DoOnClick; for example and was getting errors, did not realise the @ before the procedure call. Anyway it is working so thanks fro all your help. GetMem you are right by the way the Lazarus IDE is very good. Regards Andrew ---------------------------------------------------------------------- Thaddy · 2017-05-06 10:36 PPlz note that if your unit was in mode delphi instead of mode objfpc it would have worked without the @. ---------------------------------------------------------------------- andrew Bubble · 2017-05-06 10:42 Hi Thaddy Thanks I was not aware of that, seems there is a lot of difference between Delphi and Lazarus but I expect the underlying principles are the same. Regards Andrew ---------------------------------------------------------------------- andrew Bubble · 2017-05-06 11:28 Hi All The new component is now behaving as I hoped, I do have one more question and this concerns the Label TCaption property. If I place a TLabel on a form the Tcaption shows a StringEditor Dialog box when clicked so you can enter multiple strings if so wish. I have added a MainText : Tcaption property but this only allows the entry of a single line, how do I invoke the TSTringEditor dialoge. Regards Andrew ---------------------------------------------------------------------- wp · 2017-05-06 12:15 You must register the correct property editor for multiline strings for the type TCaption of the component, look at propedits.pp in folder components/ideintf of your Lazarus installation and scroll down to InitPropEdits for examples: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure Register;begin  RegisterComponents('MyComponents', [TLedPanel]);  RegisterPropertyEditor(TypeInfo(TCaption), TLedPanel, 'Caption', TStringMultilinePropertyEditor);end;