Missing parent in dynamically create control

The "FItemSelector" TListbox instance that is dynamically create in the TTMSFNCToolBarCustomItemPicker class in VCL.TMSFNCToolBar.pas has no parent component assigned.


This leads to unexpected behavior (crash) in other components that attempts to enumerate all controls on the form.

To fix this, edit the constructor as follow:

constructor TTMSFNCToolBarCustomItemPicker.Create(AOwner: TComponent);
begin
  inherited;
  FTimer := TTimer.Create(Self);
  FTimer.Interval := 1;
  FTimer.OnTimer := EnterTimerChanged;
  FTimer.Enabled := False;

  FEdit := TEdit.Create(Self);
  FEdit.TabStop := True;
  {$IFDEF FMXLIB}
  FEdit.Align := TAlignLayout.Client;
  FEdit.Stored := False;
  FEdit.OnChangeTracking := EditChange;
  {$ENDIF}
  {$IFDEF CMNWEBLIB}
  FEdit.Align := alClient;
  FEdit.OnChange := EditChange;
  {$ENDIF}

  Width := 100;
  Height := 24;
  HorizontalTextAlign := gtaLeading;
  FItemIndex := -1;

  FItemSelector := TListBox.Create(Self);
  FItemSelector.Parent := Self; // Assign a parent to the dynamically created control to prevent issue with control enumeration on forms
  FItemSelector.Width := 200;
  FItemSelector.Height := 150;
  {$IFDEF LCLLIB}
  FItemSelector.ClickOnSelChange := False;
  {$ENDIF}
  {$IFDEF FMXLIB}
  FItemSelector.Align := TAlignLayout.Client;
  FItemSelector.OnItemClick := ItemSelected;
  {$ENDIF}
  {$IFDEF CMNWEBLIB}
  FItemSelector.Align := alClient;
  FItemSelector.OnClick := ItemSelected;
  {$ENDIF}

  FItemSelector.OnKeyUp := ItemKeyUp;
  FItemSelector.OnKeyDown := ItemKeyDown;

  DropDownControl := FItemSelector;
  DropDownKind := ddkDropDownButton;
  DropDownWidth := FItemSelector.Width;
  DropDownHeight := FItemSelector.Height;

  FItems := TStringList.Create;
end;

Sorry, but the Listbox only has a parent assigned when the dropdown is initialized. This is by design.

Ok. My testing has shown no advert effect to the change I proposed but maybe I was wrong. Could you tell me what problem this would cause ?


In any case, couldn't the creation be deferred to the dropdown is initialized as well? We're using a set of libraries for dynamic application localisation that walks through the whole components on the form once it has been loaded in order to find and translate all strings and the fact that this TListBox is part of the created component but not the parent list is causing it to blow up.

It might be caused by the public property that gives access to the ListBox, but then again, when no parent is set, the translating libraries should take care of this. When setting the parent of TListBox at designtime this will cause unwanted behaviour. Perhaps you can workaround this by setting a parent for TListBox in the constructor of the form. We have no other reports on this behaviour.