“Control has no parent” in Create ComboBox

一个人想着一个人 提交于 2019-12-11 22:04:25

问题


In this code :

unit MSEC;

interface

uses
  Winapi.Windows, Vcl.Dialogs, Vcl.ExtCtrls, System.SysUtils, System.Classes, Vcl.Controls, Vcl.StdCtrls;

type
  TMSEC = class(TWinControl)
  private
    FOpr                  :TComboBox;
  public
    constructor Create(AOwner: TComponent); override;
  end;

implementation

const
    DEF_OPERATIONS :array[0..3] of Char = ('+', '-', '*', '/');

constructor TMSEC.Create(AOwner: TComponent);
var i         :Integer;
begin
  inherited;
  FOpr:= TComboBox.Create(Self);
  with FOpr do begin
    Parent:= Self;
    Align:= alLeft;
    Width:= DEF_OPERATIONS_WIDTH;
    Style:= csDropDownList;
    //error in next lines :
    Items.Clear;
    for i := Low(DEF_OPERATIONS) to High(DEF_OPERATIONS) do Items.Add(DEF_OPERATIONS[i]);
    ItemIndex:= 0;  
  end;
end;

end.

When I change ComboBox items, the program breaks with the message :
'Control' has no parent.
How can I fix this error or initialize ComboBox items in another way?


回答1:


TComboBox requires an allocated HWND in order to store strings in its Items property. In order for TComboBox to get an HWND, its Parent needs an HWND first, and its Parent needs an HWND, and so on. The problem is that your TMSEC object does not have a Parent assigned yet when its constructor runs, so it is not possible for the TComboBox to get an HWND, hense the error.

Try this instead:

type
  TMSEC = class(TWinControl)
  private
    FOpr: TComboBox;
  protected
    procedure CreateWnd; override;
  public
    constructor Create(AOwner: TComponent); override;
  end;

constructor TMSEC.Create(AOwner: TComponent);
begin
  inherited;
  FOpr := TComboBox.Create(Self);
  with FOpr do begin
    Parent := Self;
    Align := alLeft;
    Width := DEF_OPERATIONS_WIDTH;
    Style := csDropDownList;
    Tag := 1;
  end;
end;

procedure TMSEC.CreateWnd;
var
  i :Integer;
begin
  inherited;
  if FOpr.Tag = 1 then
  begin
    FOpr.Tag := 0;
    for i := Low(DEF_OPERATIONS) to High(DEF_OPERATIONS) do
      FOpr.Items.Add(DEF_OPERATIONS[i]);
    FOpr.ItemIndex := 0;
  end;
end;


来源:https://stackoverflow.com/questions/18991382/control-has-no-parent-in-create-combobox

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!