Reputation: 4595
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?
Upvotes: 4
Views: 1411
Reputation: 1559
When I make components with multiple parts I have not used the SetSubComponent method.
What I have done is something like this
constructor TPBxCheckBox.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FTranslateEdit := TLabeledEdit.Create(Self);
FTranslateEdit.Parent := Self;
FTranslateEdit.Visible := False;
end;
And I would try something like this
procedure TPBxCheckBox.Translate(Show: Boolean);
begin
FTranslateEdit.EditLabel.Caption := Self.Caption;
FTranslateEdit.Left := Self.Left;
FTranslateEdit.Top := Self.Top;
FTranslateEdit.Width := Self.Width;
FTranslateEdit.Height := Self.Height;
FTranslateEdit.Text := Self.Caption;
FTranslateEdit.Visible := Show;
end;
I'll improve this answer if you get me more information or if I get time to test this out.
Upvotes: 0
Reputation: 25678
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;
Upvotes: 8