Recent

Author Topic: New class with TShape and TLabel problem?  (Read 1759 times)

Jonvy

  • Full Member
  • ***
  • Posts: 102
New class with TShape and TLabel problem?
« on: September 17, 2022, 07:57:32 am »
Dear all,

I want to combine TShape and TLabel to a new class:MyShape.
Two label can represent different things, such as
'Station 10' and 'Step 1'.
Hope I can get the result as attachment.

But for my code, it can only display a TShape, no TLable appear.

Here is my code, can anyone tell me where is wrong?

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, ExtCtrls, StdCtrls;
  9.  
  10. type
  11.  
  12.   { TForm1 }
  13.  
  14.   TForm1 = class(TForm)
  15.     btnShape: TButton;
  16.     procedure btnShapeClick(Sender: TObject);
  17.     procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
  18.   private
  19.   public
  20.  
  21.   end;
  22.  
  23. type
  24.   MyShape = class(TShape)
  25.   private
  26.     stationName,stepName:TLabel;
  27.   public
  28.     property lblStation: TLabel read stationName write stationName;
  29.     property lblStep: TLabel read stepName write stepName;
  30.     constructor Create(TheOwner: TComponent);override;
  31.     procedure Paint; override;
  32.   end;
  33.  
  34. var
  35.   Form1: TForm1;
  36.   shape1:MyShape;
  37.  
  38. implementation
  39.  
  40. {$R *.lfm}
  41. constructor MyShape.Create(TheOwner: TComponent);
  42. begin
  43.   inherited Create(TheOwner);
  44.   stationName:=TLabel.Create(TheOwner);
  45.   stepName:=TLabel.Create(TheOwner);
  46.  
  47. end;
  48. procedure MyShape.Paint;
  49. begin
  50.   inherited Paint;
  51.  
  52.   stationName.Caption:='Station...';
  53.   stationName.Left:=0;
  54.   stationName.Top:=10;
  55.   stationName.Width:=50;
  56.   stationName.Height:=25;
  57.   stationName.Visible:=true;
  58.  
  59.   stepName.Caption:='Step...';
  60.   stepName.Left:=0;
  61.   stepName.Top:=45;
  62.   stepName.Width:=50;
  63.   stepName.Height:=25;
  64.   stepName.Visible:=true;
  65. end;
  66.  
  67.  
  68. { TForm1 }
  69.  
  70.  
  71. procedure TForm1.FormClose(Sender: TObject; var CloseAction: TCloseAction);
  72. begin
  73.   shape1.Free;
  74. end;
  75.  
  76. procedure TForm1.btnShapeClick(Sender: TObject);
  77.  
  78. begin
  79.   shape1:=MyShape.Create(Form1);
  80.   shape1.Shape:=TShapeType.stRectangle;
  81.   shape1.Left:=20;
  82.   shape1.Top:=130;
  83.   shape1.Width:=250;
  84.   shape1.Height:=130;
  85.   shape1.Parent:=Form1;
  86.   shape1.Visible:=true;
  87.  
  88.   shape1.lblStation.Caption:='Station 10';
  89.   shape1.lblStep.Caption:='Step 1';
  90.  
  91. end;
  92.  
  93. end.


Best regards,
Jonvy

ASerge

  • Hero Member
  • *****
  • Posts: 2469
Re: New class with TShape and TLabel problem?
« Reply #1 on: September 17, 2022, 08:30:40 am »
The shape and the labels have a common parent. Their coordinates overlap. The shape wins.

I have a few comments:
1. The base component is not a container, there will be difficulties with coordinates. It's easier to choose TWinControl and publish nested elements or just their basic properties.
2. Your independent class references the external global variable Form1.
3. Coordinates and other properties of nested objects must be set in the constructor or in Set... methods, not in the Paint procedure.

AlexTP

  • Hero Member
  • *****
  • Posts: 2649
    • UVviewsoft
Re: New class with TShape and TLabel problem?
« Reply #2 on: September 17, 2022, 09:49:35 am »
You can use TPaintBox, in its OnPaint do the work on TCanvas: paint the rectangle, then TextOut().

jamie

  • Hero Member
  • *****
  • Posts: 7392
Re: New class with TShape and TLabel problem?
« Reply #3 on: September 17, 2022, 04:24:57 pm »
This works fine in Design Mode so it should work at runtime unless something has changed since laz 2.0.4 which is what I tested this on.

 When the Shape is created in its constructor use SELF as the owner for the TLABEL controls, not the owner of the Shape because that is the form and with graphic controls things don't always follow suite as I have noted.

 It may not work because I have not tested this at dynamic time but like I said, it works in the designer so it should work at runtime, too.
Code: Pascal  [Select][+][-]
  1. constructor MyShape.Create(TheOwner: TComponent);
  2. begin
  3.   inherited Create(TheOwner);
  4.   stationName:=TLabel.Create(TheOwner); << Change to SELF
  5.   stepName:=TLabel.Create(TheOwner); << Change to self.
  6.    //Also you should set the parents here to SELF.
  7.  
  8.  
  9. end;
  10.  
  11.  
The only true wisdom is knowing you know nothing

wp

  • Hero Member
  • *****
  • Posts: 13264
Re: New class with TShape and TLabel problem?
« Reply #4 on: September 17, 2022, 07:45:50 pm »
Just added a new component to the ExCtrls package (https://sourceforge.net/p/lazarus-ccr/svn/HEAD/tree/components/exctrls/): TShapeEx, a TShape descendant with Header and Text properties. Both texts can consist of multiple lines and be left-/right-/top-/bottom-aligned/centered; margins can be specified. The unit is self-contained and can be used without the package; I am adding it here as an attachment.

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: New class with TShape and TLabel problem?
« Reply #5 on: September 17, 2022, 08:01:16 pm »
Wow, wp that was quick work.
And congratulations on the number of your posts which is reaching a notable milestone!

Jonvy

  • Full Member
  • ***
  • Posts: 102
Re: New class with TShape and TLabel problem?
« Reply #6 on: September 19, 2022, 04:10:28 pm »
Just added a new component to the ExCtrls package (https://sourceforge.net/p/lazarus-ccr/svn/HEAD/tree/components/exctrls/): TShapeEx, a TShape descendant with Header and Text properties. Both texts can consist of multiple lines and be left-/right-/top-/bottom-aligned/centered; margins can be specified. The unit is self-contained and can be used without the package; I am adding it here as an attachment.
Thanks wp. It's very good component, I'll study and use it.

AlexTP

  • Hero Member
  • *****
  • Posts: 2649
    • UVviewsoft
Re: New class with TShape and TLabel problem?
« Reply #7 on: September 19, 2022, 05:38:05 pm »
wp,
Can we have some info about ShapeEx at https://wiki.freepascal.org/ExCtrls ?

I added the initial info.
« Last Edit: September 19, 2022, 05:41:40 pm by AlexTP »

wp

  • Hero Member
  • *****
  • Posts: 13264
Re: New class with TShape and TLabel problem?
« Reply #8 on: September 19, 2022, 06:49:37 pm »
Thanks for reminding me...

Done.

AlexTP

  • Hero Member
  • *****
  • Posts: 2649
    • UVviewsoft
Re: New class with TShape and TLabel problem?
« Reply #9 on: September 19, 2022, 06:57:00 pm »
I cannot get idea of
https://wiki.freepascal.org/ExCtrls#TColumnComboBoxEx
https://wiki.freepascal.org/ExCtrls#TCheckComboBoxEx
w/o testing it. Text is blurry. Screenshot can help.

wp

  • Hero Member
  • *****
  • Posts: 13264
Re: New class with TShape and TLabel problem?
« Reply #10 on: September 19, 2022, 07:08:07 pm »
I cannot get idea of
https://wiki.freepascal.org/ExCtrls#TColumnComboBoxEx
https://wiki.freepascal.org/ExCtrls#TCheckComboBoxEx
w/o testing it.
Described in the first paragraph of each section.

Text is blurry.
Don't understand... Which text?

Screenshot can help.
There is one in the wiki article for the TCheckComboBoxEx. And the attachment contains one for the TColumnComboBoxEx (not a very nice one, though...).


 

TinyPortal © 2005-2018