2006. január 14., szombat

Designer instance for DataModules


Problem/Question/Abstract:

I can retreive instance of Designer for forms as their property "Designer", but how to retreive it for DataModule like objects?

Answer:

For receiving instance of Designer for forms you should just get form's property "Designer". But how to receive the Designer instance if you have only DataModule's instance?

It is very simply: the DataModule is an usual VCL component and its owner is hidden form, so for retreiving Designer instance of DataModule just retreive "Designer" property of DataModule's owner.
The function below demonstrates retreiving Designer instance for any form in your project by its name. This function is useful when Designer instance is necessary for your experts.

Add these definitions:

{$IFDEF VER120}
TFormDesigner = IFormDesigner;
{$ENDIF}
{$IFDEF VER125}
TFormDesigner = IFormDesigner;
{$ENDIF}
{$IFDEF VER130}
TFormDesigner = IFormDesigner;
{$ENDIF}

function GetDesigner(FToolServices: TIToolServices; FormName: string): TFormDesigner;
var
  tmpC: TComponent;
begin
  Result := nil;
  with FToolServices.GetFormModuleInterface(FormName).GetFormInterface.GetFormComponent
    do
  begin
    tmpC := GetComponentHandle;
    if (tmpC is TCustomForm) then
    begin
      // We have the usual form
      Result := TFormDesigner(TCustomForm(tmpC).Designer);
      Exit;
    end
    else
      // We have the DataModule or WebModule or something else
      Result := TFormDesigner(TCustomForm(tmpc.Owner).Designer);
  end;
end;

For receiving Designer without using ToolServices you can use following function:

finction GetDesigner(AComp: TComponent): TFormDesigner;
var
  tmpC: TComponent;
begin
  Result = nil;
  if not Assigned(AComp) then
    Exit;
  if AComp is TCustomForm then
    Result := TFormDesigner(TCustomForm(AComp).Designer)
  else
  begin
    tmpC := AComp;
    while true do
    begin
      if Assigned(tmpC.Owner) then
        tmpC := tmpC.Owner
      else
        break;
      if tmpC is TCustomForm then
      begin
        Result := TFormDesigner(TCustomForm(AComp).Designer);
        break;
      end;
    end;
  end;
end;

Nincsenek megjegyzések:

Megjegyzés küldése