Einzelnen Beitrag anzeigen

daniel775

Registriert seit: 27. Nov 2010
46 Beiträge
 
#3

AW: Komponente entwickeln mit TCollection, TCollectionItem PropertyEditor

  Alt 17. Mär 2013, 19:06
Irgendwie stehe ich auf dem Schlauch...

Ich habe jetzt in den Properties eine Form und kann dort auch die Eigenschaften bearbeiten. Aber wie könnte ich mir diese in den Designer holen? Ist das so überhaupt machbar?

Wenn ich mir die Kompo auf eine Form ziehe und nicht vor dem schließen des Projekts runter nehme, dann bekomme ich auch eine invalid Pointer Exception!?

@stahli:
Wäre gut wenn der Link zu dem Video erneuert würde.

Hier einwenig Code und im Anhang das Package (in D7 erstellt)

Code:

package MyFormCompo;

{$R *.res}
{$ALIGN 8}
{$ASSERTIONS ON}
{$BOOLEVAL OFF}
{$DEBUGINFO ON}
{$EXTENDEDSYNTAX ON}
{$IMPORTEDDATA ON}
{$IOCHECKS ON}
{$LOCALSYMBOLS ON}
{$LONGSTRINGS ON}
{$OPENSTRINGS ON}
{$OPTIMIZATION ON}
{$OVERFLOWCHECKS OFF}
{$RANGECHECKS OFF}
{$REFERENCEINFO ON}
{$SAFEDIVIDE OFF}
{$STACKFRAMES OFF}
{$TYPEDADDRESS OFF}
{$VARSTRINGCHECKS ON}
{$WRITEABLECONST OFF}
{$MINENUMSIZE 1}
{$IMAGEBASE $400000}
{$DESCRIPTION 'TmyFormComponent mit PropertyEditor'}
{$IMPLICITBUILD OFF}

requires
  designide,
  vcl,
  rtl,
  vclactnband,
  vclx;

contains
  myFormComponent_dsgn in 'myFormComponent_dsgn.pas',
  myFormComponent in 'myFormComponent.pas';

end.
Code:

unit myFormComponent_dsgn;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Buttons, DesignIntf, DesignEditors, myFormComponent;

type
  TmyFormPropEditor = class(TClassProperty)
  protected
    FForm: TMyForm;
    function GetForm: TMyForm;
    procedure SetForm(const Value: TMyForm);
  public
    procedure Edit; override;
    function GetAttributes: TPropertyAttributes; override;
  end;

procedure Register;

implementation


procedure Register;
begin
  RegisterPropertyEditor(TypeInfo(TMyForm), TmyFormComponent, 'Form', TmyFormPropEditor);
end;

{ TmyFormPropEditor }

procedure TmyFormPropEditor.Edit;
begin
  inherited;
  if GetForm = nil then
  begin
    FForm := TMyForm.create(nil);
    SetForm(FForm);
  end;

  with TmyFormPropEditor.Create(nil, 0) do
  try
  finally
    Free;
  end;

end;

function TmyFormPropEditor.GetAttributes: TPropertyAttributes;
begin
  Result := inherited GetAttributes + [paDialog];
end;

function TmyFormPropEditor.GetForm: TMyForm;
begin
  if GetOrdValue <> 0 then
  begin
    Result := TMyForm(GetOrdValue);
  end
  else
  begin
    Result := nil;
  end;
end;

procedure TmyFormPropEditor.SetForm(const Value: TMyForm);
begin
  SetOrdValue(Longint(Value));
end;

end.
Code:

unit myFormComponent;

interface

uses
  SysUtils, Classes, Controls, StdCtrls, forms;

type

  TMyForm = class(TForm)
  public
    function getDisplayValue: string;
  end;

  TMdMyItem = class (TCollectionItem)
  private
    FCode: Integer;
    FForm: TCustomForm;
    procedure SetForm(const Value: TCustomForm);
  published
    property Form: TCustomForm read FForm write SetForm;
  end;

  TMdMyCollection = class (TCollection)
  private
    FComp: TComponent;
    FCollString: string;
  public
    constructor Create (CollOwner: TComponent);
    function GetOwner: TPersistent; override;
    procedure Update(Item: TCollectionItem); override;
  end;

  TmyFormComponent = class(TComponent)
  private
    FForm: TMyForm;
    FColl: TMdMyCollection;
    function GetCollString: string;
    procedure SetMoreData(const Value: TMdMyCollection);
  protected
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
  published
    property Form: TMyForm read FForm write FForm;
    property MoreData: TMdMyCollection read FColl write SetMoreData;
    property CollString: string read GetCollString;
  end;

procedure Register;

implementation

procedure Register;
begin
  RegisterComponents('Beispiele', [TmyFormComponent]);
end;


{ TMyForm }

function TMyForm.getDisplayValue: string;
begin
  result := TMyForm.ClassName;
end;


{ TMdMyCollection }

constructor TMdMyCollection.Create (CollOwner: TComponent);
begin
  inherited Create (TMdMyItem);
  FComp := CollOwner;
end;

function TMdMyCollection.GetOwner: TPersistent;
begin
  Result := FComp;
end;

procedure TMdMyCollection.Update(Item: TCollectionItem);
var
  str: string;
  i: Integer;
begin
  inherited;
  // update everything in any case...
  str := '';
  for i := 0 to Count - 1 do
  begin
    str := str + (Items [i] as TMdMyItem).ClassName;
    if i < Count - 1 then
      str := str + '-';
  end;
  FCollString := str;
end;


{ TMdMyItem }

procedure TMdMyItem.SetForm(const Value: TCustomForm);
begin
end;


{ TmyFormComponent }

constructor TmyFormComponent.Create(AOwner: TComponent);
begin
  inherited;
  FForm := TMyForm.Create(AOwner);
  FColl := TMdMyCollection.Create(Self);
end;

destructor TmyFormComponent.Destroy;
begin
  FForm.Free;
  FForm := nil;
  FColl.Free;
  inherited;
end;

procedure TmyFormComponent.SetMoreData(const Value: TMdMyCollection);
begin
  FColl.Assign(Value);
end;

function TmyFormComponent.GetCollString: string;
begin
  Result := FColl.FCollString;
end;

end.
Angehängte Dateien
Dateityp: zip MyFormComponent.zip (36,1 KB, 6x aufgerufen)
  Mit Zitat antworten Zitat