Halil Han BADEM
Halil Han BADEM

Reputation: 199

Delphi - Create Component Sub Property

I am developing components with Delphi. I'm stuck on something and need your help. I needed a sub property when creating the property value in my component.

enter image description here

In short, I wanted it to be like this on the ide. I wrote the following sample codes for this. I encountered a problem that I could not understand in the code I wrote for testing purposes. my codes;

type
 TCheckValue = class(TPersistent)
  private
   FValue1: Boolean;
   FValue2: Boolean;
   FOnChange: TNotifyEvent;
   procedure Changed;
   procedure SetValue1(Value: Boolean);
   procedure SetValue2(Value: Boolean);
  public
   procedure Assign(Source : TPersistent); override;
   property OnChange : TNotifyEvent read FOnChange write FOnChange;
  published
   property Value1: Boolean read FValue1 write SetValue1 default true;
   property Value2: Boolean read FValue2 write SetValue2 default true;
 end;

Component:

type
 THBComp = class(TComponent)
    constructor create;
    destructor destroy; override;
   private
    FCheckValue: TCheckValue;
    procedure setCheckValue(Value: TCheckValue);
   published
    property CheckValue: TCheckValue read FCheckValue write setCheckValue;
 end;

TCheckValue procedure code:

procedure TCheckValue.Changed;
begin
  if Assigned(FOnChange) then
  begin
    FOnChange(Self);
  end;
end;

procedure TCheckValue.SetValue1(Value: Boolean);
begin
  if FValue1 <> Value then
  begin
   FValue1 := Value;
   Changed;
  end;
end;

procedure TCheckValue.SetValue2(Value: Boolean);
begin
  if FValue2 <> Value then
  begin
   FValue2 := Value;
   Changed;
  end;
end;

procedure TCheckValue.Assign(Source : TPersistent);
begin
    if Source is TCheckValue then
  begin
    FValue1 := TCheckValue(Source).FValue1;
    FValue2 := TCheckValue(Source).FValue2;
    Changed;
  end else
    inherited;
end;

The result I got with these codes is as follows:

enter image description here

so Value1 and Value2 are not displayed on the IDE side. Do I have an incomplete definition for this? Is there any documentation for this topic? I think there is a problem with my research words or there is a document problem.

IDE Version: 10.4.2

I'm sorry for my bad english. Thank you so much.

Upvotes: 1

Views: 723

Answers (1)

Remy Lebeau
Remy Lebeau

Reputation: 595827

TCheckValue is implemented correctly. The problem is your THBComp constructor is not override'ing the TComponent constructor, so your TCheckValue object is not created when a THBComp object is placed on a Form at design-time, or streamed in from a DFM. You need to implement that constructor, eg.

type
  THBComp = class(TComponent)
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
  private
    FCheckValue: TCheckValue;
    procedure setCheckValue(Value: TCheckValue);
  published
    property CheckValue: TCheckValue read FCheckValue write setCheckValue;
  end;

...

constructor THBComp.Create(AOwner: TComponent);
begin
  inherited;
  FCheckValue := TCheckValue.Create;
end;

destructor THBComp.Destroy;
begin
  FCheckValue.Free;
  inherited;
end;

procedure THBComp.setCheckValue(Value: TCheckValue);
begin
  FCheckValue.Assign(Value);
end;

Upvotes: 5

Related Questions