Einzelnen Beitrag anzeigen

Benutzerbild von geskill
geskill

Registriert seit: 17. Feb 2007
Ort: NRW
420 Beiträge
 
Delphi 2010 Professional
 
#4

Re: Ungültige Zeigeroperation Plugin mit interface

  Alt 7. Jul 2009, 20:43
Das ist ja blöd!

Ich habe mehrere eigene Controls (TIEdit, TIComboBox...) die alle von TIBasic abstammen. Das interface von IBasic kann man nun ja noch ganz einfach erstellen und die TIBasic deklaration umschreiben.

TIBasic = class(TInterfacedObject, IBasic) aber! wie sieht die von TIEdit aus?
so sieht es aktuell aus:
TIComboBox = class(TIBasic) Außerdem müsste man ja im interface die Funktion Free zum freigeben der Controls deklarieren
Hier habe ich mal einen größeren Teil des Komponenten Managers, damit versteht man es vielleicht etwas besser:
Delphi-Quellcode:
type
  TComponentManager = class
    private
      FWorkPanel:TComponent;
      FControlList:TInterfaceList;
      type
        TIBasicMeta = class of TIBasic;
      function GetControl(Index: Integer):IBasic;
      procedure SetControl(Index: Integer; AControl: IBasic);
      procedure DisposeControls;
      function GetClassType(AType: TComponentID): TIBasicMeta;
    public
      constructor Create;
      property WorkPanel:TComponent read FWorkPanel write FWorkPanel;
      procedure NewControl(AType:TComponentID;
                           AName,ATitle,AValue,AHint,AList:string;
                           ALeft,ATop,AWidth,AHeight:Integer); overload;
      procedure NewControl(AClass: TIBasicMeta; AType: TComponentID;
                           AName,ATitle,AValue,AHint,AList:string;
                           ALeft,ATop,AWidth,AHeight:Integer); overload;
      function ReadControl(const Id:Integer):TControl; overload;
      function ReadControl(const Name:String):TControl; overload;
      property Controls:TInterfaceList read FControlList;
      property Control[Index:Integer]:IBasic read GetControl write SetControl;
      function ControlCount:Integer;
      destructor Destroy; override;
  end;

implementation

uses
  // Api controls
  uIEdit,uIComboBox,uIComboBoxList,uIPicture;

procedure TComponentManager.DisposeControls;
var I:Integer;
begin
  for I := FControlList.Count -1 downto 0 do
  begin
    case IBasic(FControlList.Items[I]).IComponent of
      cEdit: TIEdit(FControlList.Items[I]).Free; // error... klar, müsste mit IEdit zugreifen
      cComboBox: TIComboBox(FControlList.Items[I]).Free;
      cComboBoxList: TIComboBoxList(FControlList.Items[I]).Free;
      cPicture: TIPicture(FControlList.Items[I]).Free;
    end;
    FControlList.Delete(I);
  end;
end;

function TComponentManager.GetClassType(AType: TComponentID): TIBasicMeta;
begin
  case AType of
    cEdit: result := TIEdit;
    cComboBox: result := TIComboBox;
    cComboBoxList: result := TIComboBoxList;
    cPicture: result := TIPicture;
  else
    raise Exception.Create('Unknown component');
  end;
end;

procedure TComponentManager.NewControl;
begin
  NewControl(GetClassType(AType),AType,AName,ATitle,AValue,AHint,AList,ALeft,ATop,AWidth,AHeight);
end;

procedure TComponentManager.NewControl;
var lBasic:IBasic;
begin
  lBasic := AClass.Create(TWinControl( FWorkPanel )); // funktioniert seltsamerweise wunderbar
  with lBasic do
  begin
    IComponent := AType;
    Name := AName;
    Title := ATitle;
    Hint := AHint;
    Left := ALeft;
    Top := ATop;
    Width := AWidth;
    Height := AHeight;
    Value := AValue; // abstract
  end;

  case AType of
    cComboBox: TIComboBox(lBasic).List := AList; // error... klar, müsste mit IComboBox zugreifen
    cComboBoxList: TIComboBoxList(lBasic).List := AList;
  end;

  FControlList.Add( lBasic );
end;
Das schreiben und lesen mit dem Plugin funktioniert jetzt.
Schön ist das ganze aber nicht vorallem gibt es die TInterfaceList wahrscheinlich nur in Delphi
Delphi-Quellcode:
procedure Tblabla.Exec(const Controls:TInterfaceList);
var s:string;
begin
  ShowMessage(IBasic(Controls.Items[0]).Value //+ #13 +
              );

  s := 'hans';

  IBasic(Controls.Items[1]).Value := s;

  //ComponentManager.Control[0].Value := s;
end;
Sebastian
  Mit Zitat antworten Zitat