Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Constraint component parent on creation

I want to be able to limit where a component is created.

Like for instance, TMyChild could be a TButton, and TMyParent could be a TPanel, and when I drop MyChild onto some other component I want MyChild to check if it is being created in a TMyParent/TPanel or not.

If it is, then fine lets do it, if it is NOT created in a TMyParent/TPanel then cancel the TMyChild creation and show a message that says something like: "Sorry, MyChild needs to be created in MyParent!".

Thank you!

like image 911
xaid Avatar asked Aug 26 '26 16:08

xaid


1 Answers

You must override the Controls.TControl.SetParent method.

  TMyChild = class(TControl)
  protected
    procedure SetParent(AParent: TWinControl); override;
  end;


procedure TMyChild.SetParent(AParent: TWinControl);
begin
  if (AParent <> nil) then
  begin
    if not (AParent is TMyParent) then
      raise Exception.CreateFmt('Sorry, MyChild needs to be created in MyParent!', [ClassName]);
  end;
  inherited;
end;
like image 96
RRUZ Avatar answered Aug 28 '26 05:08

RRUZ



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!