[SOLVED] Issues with subclass creation lazarus / 31-LCL ---------------------------------------------------------------------- pusuni · 2014-09-23 13:57 Hi! I'm trying to make a new  control (class) derivated from TCustomPanel class. A easy and simplified code  version of this class   is: Code: [Select]unit Test; {$mode objfpc}{$H+} interface uses   Classes, SysUtils, LResources, Forms, Controls, Graphics, Dialogs, ExtCtrls,   StdCtrls, LMessages; type   TTest = class(TCustomPanel)   private     { Private declarations }     ControlOne              : TEdit;     ControlTwo              : TLabel;   protected     { Protected declarations }   public     { Public declarations }       constructor Create (AOwner : TComponent) ;   override;       destructor  Destroy;                         override;   published     { Published declarations }   end; procedure Register; implementation procedure Register; begin   RegisterComponents('Standard',[TTest]); end; constructor TTest.Create (AOwner : TComponent) ; begin   inherited Create (AOwner);   Height := 136;   Width := 176;   ClientHeight := 132;   ClientWidth := 172;   Constraints.MaxHeight:=136;   Constraints.MaxWidth :=176;   Constraints.MinHeight:=136;   Constraints.MinWidth :=176;   CAPTION :='';   ControlOne:= TEdit.Create(Self);   ControlOne.Parent:= Self;   ControlOne.SetSubComponent(true);   ControlOne.ControlStyle := ControlOne.ControlStyle - [csNoDesignSelectable];   ControlOne.Name := 'Input1';   ControlOne.Text := '1';   ControlOne.Left := 36;   ControlOne.Height := 23;   ControlOne.Top := 20;   ControlOne.Width := 130;    ControlTwo := TLabel.Create(Self);    ControlTwo.Parent:= Self;    ControlTwo.SetSubComponent(true);    ControlTwo.ControlStyle := ControlTwo.ControlStyle - [csNoDesignSelectable];    ControlTwo.Name := 'Label1';    ControlTwo.Caption := 'Label1'; end; destructor  TTest.Destroy; begin    ControlTwo.Free;    ControlOne.Free;    inherited Destroy; end; end. That code compile OK and I can create a new package with this new class: TTest. I can create a new form with this new control, but, I get a problem. TCustomPanel has a default "Caption text" , I don't want that text. Inside  the constructor,  I added the following code to delete that text:   CAPTION :=''; but the text is always on . How can I delete that text? Thanks in advanced ---------------------------------------------------------------------- Blaazen · 2014-09-23 14:26 It works well on Linux (both GTk2 and Qt4). Also Wine is OK. With or without the line: Code: [Select]CAPTION :=''; ---------------------------------------------------------------------- Mike.Cornflake · 2014-09-23 14:27 I'm guessing that at the point in the creation scheme the underlying widgetset instance of TPanel hasn't been created, so TControl.SetText (which calls TControl.RealSetText) has no handle to send the new caption message to.  (only wildly guessing). For your reference, I found these notes in Controls.pp Code: [Select]{* Note on TControl.Caption  * The VCL implementation relies on the virtual Get/SetTextBuf to  * exchange text between widgets and VCL. This means a lot of  * (unnecessary) text copies.  * The LCL uses strings for exchanging text (more efficient).  * To maintain VCL compatibility, the virtual RealGet/SetText is  * introduced. These functions interface with the LCLInterface. The  * default Get/SetTextbuf implementation calls the RealGet/SetText.  * As long as the Get/SetTextBuf isn't overridden Get/SetText  * calls RealGet/SetText to avoid PChar copying.  * To keep things optimal, LCL implementations should always  * override RealGet/SetText. Get/SetTextBuf is only kept for  * compatibility.  }          I simply cannot find where Caption is defined.  So for now, try just overriding RealSetText, and always set '' ---------------------------------------------------------------------- marcov · 2014-09-23 14:37 I'm no pro in making designtime components, but: 1. make sure that after the constructor change your recompile everything (package and thus Lazarus) 2. existing dropped ttest components will already have the caption in their streaming, and are thus unchanged. Drop new ones to test. ---------------------------------------------------------------------- ChrisF · 2014-09-23 14:42 @Mike.Cornflake Or the default value caption is set after the control creation (which is usually the case for any registered control, during the streaming). So emptying it in the constructor doesn't have any visible effect. Anyway, overriding the set text method seems also to be the correct approach to me. ---------------------------------------------------------------------- Blaazen · 2014-09-23 14:51 You can try: Code: [Select]  ControlStyle := ControlStyle - [csSetCaption]; ---------------------------------------------------------------------- howardpc · 2014-09-23 16:05 AFAIK csSetCaption applies to design-time behaviour, not runtime behaviour. Setting Caption to EmptyStr in the constructor or overriding RealSetText and doing the same should both achieve the desired effect. If not, I think Marco has explained why already dropped components show 'old' behaviour. The TTest as written shows no Caption here (Win32). ---------------------------------------------------------------------- Blaazen · 2014-09-23 17:01 QuoteAFAIK csSetCaption applies to design-time behaviour, not runtime behaviour.It's not clear from OP if it is design-time or run-time issue. He wrote: QuoteThat code compile OK and I can create a new package with this new class: TTest. I can create a new form with this new control, but, I get a problem. ---------------------------------------------------------------------- pusuni · 2014-09-24 09:31 Hi everyone!! Hi Howard!! Sorry for not giving all the information: the problem it is design- time and Quote from: Blaazen on September 23, 2014, 04:51:25 pmYou can try: Code: [Select]  ControlStyle := ControlStyle - [csSetCaption]; that fixed my issue. Thanks at all ---------------------------------------------------------------------- pusuni · 2014-09-24 09:37 Quote from: ChrisF on September 23, 2014, 04:42:05 pm the default value caption is set after the control creation (which is usually the case for any registered control, during the streaming). So emptying it in the constructor doesn't have any visible effect. I didn't know it but that's the point!! Thanks, ChrisF ---------------------------------------------------------------------- pusuni · 2014-09-24 09:47 Quote from: Mike.Cornflake on September 23, 2014, 04:27:27 pmI simply cannot find where Caption is defined.  So for now, try just overriding RealSetText, and always set '' You are right, but if you compile and run, there is a caption in design time (look at the picture): http://subefotos.com/ver/?9cb641d81c449363ae6ff661cf5f3541o.jpg Anyway, the Blaazen point has fixed my problem Thanks