Foros Club Delphi

Foros Club Delphi (https://www.clubdelphi.com/foros/index.php)
-   OOP (https://www.clubdelphi.com/foros/forumdisplay.php?f=5)
-   -   Exception class EInvvalidOperation... (https://www.clubdelphi.com/foros/showthread.php?t=48672)

MON___ 01-10-2007 21:38:36

Exception class EInvvalidOperation...
 
Estoy creando un sencillo componente descendiente de TCustomComboBox; intento cargar en un combo las provincias o las regiones españolas ordenadas alfabéticamente.

Al cargar los nombres (items.loadFromStream) me da el error: Exception class EInvalidOperation with message 'Control' has no parent window

He aquí el código fuente:

Código Delphi [-]
implementation

{$R *.dfm}
{$R hispania.res}

  // TCustomComboHispania
  type
    TCustomComboHispania = class(TCustomComboBox)
    private
      FProvincias : boolean;
      procedure SetProvincias(Value : boolean);
    protected
    public
      constructor Create(AOwner : TComponent); override;
      property Provincias : boolean read FProvincias write SetProvincias; 
    published
      
  end;

  constructor TCustomComboHispania.create(AOwner : TComponent);
  begin
    inherited Create(AOwner);
    style := csDropDownList;
    ShowHint := True;
    Sorted := false;
    Provincias := True;
  end;

  procedure TCustomComboHispania.SetProvincias(Value : boolean);
    var
     R : TResourceStream;
  begin
    FProvincias := Value;
    R := TResourceStream.create(hInstance, 'PROVINCIAS_ESPANNOLAS', RT_RCDATA);
    try
      items.loadFromStream(R);
    finally
      R.Free;
    end;

  end;

 /////////////////////////////////////////////////////////////////////////////

 // PROBANDO EL OBJETO ANTES DE INSTALARLO EN LA PALETA DE COMPONENTES

 var
  Combo : TCustomComboHispania;

procedure TForm1.Button1Click(Sender: TObject);
begin
  Combo := TCustomComboHispania.create(form1);
  Combo.left := 30;
  Combo.Top := 30;
  Combo.parent := self;
end;

end

xEsk 03-10-2007 01:25:15

El problema, está en que intentas cargas los items antes de que esté "configurado del todo".

Haciéndolo así, sí que funciona (debes añadir los items en "CreateWnd"):
Código Delphi [-]
implementation

{$R *.dfm}
{$R hispania.res}

  // TCustomComboHispania
  type
    TCustomComboHispania = class(TCustomComboBox)
    private
      FProvincias : boolean;
      procedure SetProvincias(Value : boolean);
    protected
      procedure CreateWnd; override; // añade esto
    public
      constructor Create(AOwner : TComponent); override;
      property Provincias : boolean read FProvincias write SetProvincias; 
    published
      
  end;

  constructor TCustomComboHispania.create(AOwner : TComponent);
  begin
    inherited Create(AOwner);
    style := csDropDownList;
    ShowHint := True;
    Sorted := false;
  end;

  procedure TCustomComboHispania.CreateWnd;
  begin
    inherited CreateWnd;
    Provincias := True; // aqui si puedes, decirle que añada las provincias
  end;

  procedure TCustomComboHispania.SetProvincias(Value : boolean);
    var
     R : TResourceStream;
  begin
    FProvincias := Value;
    R := TResourceStream.create(hInstance, 'PROVINCIAS_ESPANNOLAS', RT_RCDATA);
    try
      items.loadFromStream(R);
    finally
      R.Free;
    end;

  end;

 /////////////////////////////////////////////////////////////////////////////

 // PROBANDO EL OBJETO ANTES DE INSTALARLO EN LA PALETA DE COMPONENTES

 var
  Combo : TCustomComboHispania;

procedure TForm1.Button1Click(Sender: TObject);
begin
  Combo := TCustomComboHispania.create(form1);
  Combo.left := 30;
  Combo.Top := 30;
  Combo.parent := self;
end;

end

MON___ 03-10-2007 13:54:38

¡Gracias, xEsk; ésa es la solución! :):):)


La franja horaria es GMT +2. Ahora son las 05:18:09.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Traducción al castellano por el equipo de moderadores del Club Delphi