Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delphi: How to show a compound component?

Tags:

delphi

I have a check box control with a labeled edit as a published subcomponent.

What I'm trying to do is create a Translate procedure for the check box that would show the labeled edit on top, and allow the user to change the text of the check box's caption. Something like this:

constructor TPBxCheckBox.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  FTranslateEdit := TLabeledEdit.Create(Self);
  FTranslateEdit.Parent := Self.Parent;
  FTranslateEdit.SetSubComponent(True);
  FTranslateEdit.Visible := False;
end;    

procedure TPBxCheckBox.Translate(Show: Boolean);
begin
  TranslateEdit.Left := Self.Left;
  TranslateEdit.Top := Self.Top;
  TranslateEdit.EditLabel.Caption := Self.Caption;
  TranslateEdit.Text := Self.Caption;
  TranslateEdit.Visible := Show;
  TranslateEdit.Width := Self.Width;
end;

But this doesn't work - the labeled edit is never shown.

What am I doing wrong here?

like image 267
croceldon Avatar asked Feb 24 '23 11:02

croceldon


1 Answers

It doesn't show because at TPBxCheckBox.Create() time Parent isn't yet assigned, so you're basically doing TranslateEdit.Parent := nil;.

If you really want your TranslatedEdit to have the same parent as the TPBxCheckBox itself, you could override SetParet and take action at the moment TPBxCheckBox's Parent is Assigned. Something like this:

TPBxCheckBox = class(TWhatever)
protected
  procedure SetParent(AParent: TWinControl); override;
end;

procedure TPBxCheckBox.SetParent(AParent: TWinControl);
begin
  inherited;
  TranslatedEdit.Parent := AParent;
end;
like image 189
Cosmin Prund Avatar answered Mar 02 '23 17:03

Cosmin Prund